Feedback Form Validation
What is validation?
- Validation is a process of making sure that forms are filled out correctly when they are submitted.
- For example, e-mail addresses have a specific format that must be followed. Another might be that a field is not optional, so it should not be left empty.
- Go to this example page and try to fill out the form with invalid data (bad e-mail address, letters in the phone number, empty name, mismatching passwords, etc).
- Notice how it tells you you've done something wrong before you even submit! This is done with JavaScript.
- Don't worry about the code below. They are using a certain library to do this, and we will use a different one.
- Go ahead and close that page.
LiveValidate
- It's possible to write validation JavaScript from scratch, but it's a pain! Let's use a library.
- Go to Live Validation.
- Go to the downloads page and save the standalone package as "livevalidation.js" in your feedback folder.
- Open the HTML of your feedback form to edit.
- Add two script tags and an onload event like those below.
- Notice that one of the script tags is for the file we just saved. The other is for one we are about to create.
validate.js
- Open a new Notepad and save it as "validate.js" in your feedback folder.
- Add the code that defines the loadValidation function:
- Take a moment to look at this code and see if you can figure it out.
- So far, this code tests whether the name has proper characters and if it is the right length.
- Test your form using the list of names below. If there are any that you think should work, adjust the validation code until you get the desired result.
- Also, don't you think the name field should be required? What happens if it's empty? We should fix that.
Adding Validation Requirements
- Go back to the Live Validation Web site.
- Click on "documentation."
- Look for examples of how to use the code that we are already using, in order to understand it better.
- Then, look at the documentation for how to use the Presence function.
- Finish the validation of the name field by making it required.
Extra Time Today
- Read this article to find out some design principles of using JavaScript validation.
- This site provids a chart of regex pattern codes for Format matchin.