Form validation is an essential part of any production application. Being clear with users about what information they are required to enter on a given form, and in what format, will help ensure that the form filling process is smooth and frustration-free. Typically form validation should be done both before the user submits the form (on the front end), as well as after the form has been submitted (on the back end) to ensure that the information really does meet all your requirements, but we'll just be looking at front end validation here.
For this exercise, we will use the jQuery Validation plugin to help make form validation easier to set up. This plugin allows us to set validation rules, error messages, and validation behavior for form elements in a simple, predictable way. Keep the plugin documentation open, and follow these steps to complete the exercise:
-
Open the index.html file under the
exercise
folder in your browser. -
Open the index.html file in your code editor.
-
Include the jQuery Validation source in the index file, either by linking to a CDN or by downloading it and including the local file.
-
In your js/script.js file, add the standard
$(document).ready()
statement. -
Inside the
$(document).ready()
body, select the form using jQuery ($('form')
will do) and initialize the validate plugin on it. -
Look at the index.html file in your browser. For all fields marked with an asterisk, add
data-rule-required="true"
to each correspondinginput
element. Do not do this forinput[type="radio"]
elements. Adding this attribute tells the form validator that these fields should have values before the form can be submitted. Once you have done that, reload the page, submit the empty form, and see what happens. -
For each of the mandatory fields, create a custom error message by adding
data-msg-required="[your message, without brackets]"
to the input element. Reload the page, submit the empty form, and see your new messages in action. -
The validation plugin offers preset validation rules for common types of form inputs. For the email address field, add a rule using the
data-rule-x
attribute format that we used earlier to ensure the field is formatted as an email address. Also add an error message for this rule that lets the user know they need to enter an email address. -
Now add
data-rule-required="true"
to the radio button elements. Also add asterisks next to the field labels to indicate to the user that these fields are required. Watch what happens when the form is submitted without having selected an option for either field. Gross. -
To fix the error message placement for these fields, inspect the error message in your browser and copy the element that contains the message. Copy and paste that markup below the closing tag for the last
<div class="radio">
, and remove theid
attribute and message. Next time you try to submit the form, the error message should display nicely below the radio buttons. When displaying the error message for a field, the plugin looks for an element with a class oferror
, and if it doesn't find one for that field, it creates one dynamically. By placing an error element in the markup, we're telling the plugin to put error messages inside that element rather than determining the message placement on its own. Thefor
attribute tells the plugin thename
of the element that has the error. -
Experiment with other validation rules -- try adding maximum or minimum length rules and corresponding custom error messages for different fields.
-
Look through the documentation for the validate method and turn off validation checks on keyup.
There are many more options and settings to explore with this plugin, but this should give you a general idea of how validation works and how the jQuery Validation plugin makes our lives easier.