Introduction
Ah... form validation. A subject that has been the source of debates ever since Web browsers were born. There are as many ways to implement form validation as there are opinions of the best way to do it. This article discusses client-side validation using jQuery's validation plugin. That is, we will use JavaScript to validate the fields before submitting the form to the server. This is fast and efficient and provides quick replies to your visitor in the event of any errors. However, it is advisable to also validate the data on the server-side before adding it into the database.
A Simple Form
Let us start with a simple example. Our demonstration form contains four fields: name, e-mail, comment and URL. As you can see, the first three fields are required, whereas the URL field is optional. If you submit the form without filling in the required fields, you will be prompted with an error message.
» View the demonstration form.
The code is quite straightforward and doesn't need much explanation. However, there are a few important points that I would like to bring to your attention.
- Remember to include the the jQuery library and the bassistance validation plugin. The link to the jQuery library must come first in the
head
section, otherwise the validation will not work. - Remember to assign the
id
attribute of the form and thename
attribute of all the fields that you want to validate.
Now, let us see the validate()
function in detail.
All we are doing here is initializing the validation of the form using the validate()
function. It can take several parameters. In the example above, we use only two of them, but you can find a list of all the options for the validate()
function at https://docs.jquery.com/Plugins/Validation/validate#toptions.
The two parameters we used are:
rules
: allows you to specify which fields you want to validate. In this case, we are validatingname
,email
,url
andcomment
. For each field, we specify if the field is required (required: true
). We can specify that it must be a valid e-mail address, URL, etc.messages
: allows you to specify the error message for a particular field (like the comments field in the example above). If you don't specify it, a default message is provided that says "this field is required".
In the example above, we only used three validation methods (required
, email
and url
). There are several other methods that can be used here. Here are a few of them:
remote
: requests a resource to check the element for validity.min
: makes the element require a given minimum.date
: makes the element require a date.creditcard
: makes the element require a credit card number.equalTo
: requires the element to be the same as another one.
You can find an exhaustive list of built-in validation methods at https://docs.jquery.com/Plugins/Validation.
"But I can't find a suitable built-in method for my situation. Can I write my own method?" Yes you can! And its really easy.
Writing Your Own Validation Method
Consider a form where the user chooses his favorite sport. If he submits the form without choosing a sport, he is prompted with an error message.
As there is no built-in method to validate a drop down menu, we need to define our own method.
We do so by calling the jQuery.validator.addMethod()
method. It takes three parameters:
name
: The name of the method, used to identify and referencing it, must be a valid javascript identifier.method
: the actual method implementation, returning true if an element is valid.message
: The default message to display for this method.
In the validate function, we specify that the 'sport' field should be validated using the selectNone
method.
Being able to define your own validation methods allows you to validate pretty much anything that you can think of. Using this plugin, you can very quickly add validation to a form. However, let me remind you that this validation happens only on the client side. While JavaScript validation is useful because of its responsiveness, if there is a JavaScript error, the browser could ignore the remaining validation and submit the form to the server. So my final word of advice would be to validate the data again on the server side.
Original: February 26, 2009