WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe

Newletters

Form Validation using jQuery

By

Social Bookmark

NOC Engineer (NYC)
Next Step Systems
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume



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.

Here is the code we used for the form in the demonstration above.

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.

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 http://docs.jquery.com/Plugins/Validation/validate#toptions.

The two parameters we used are:

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:

You can find an exhaustive list of built-in validation methods at http://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:

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

Social Bookmark

Recent Articles

WebReference.com site name
Build a Searchable Online Employee Directory with ASP.NET
Top 10 New Features in CSS 3
Fine Tuning Your Java 6 Database-Driven ResourceBundle
internet.com site name
FCoE -- the Glue That Holds Networking and Storage Together
The Evolution of Phone-based Applications
Is Cisco's WPA Migration Mode Leaving Wi-Fi Users at Risk?


Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers