cameras in the cloud
this is our weblog. it includes ramblings on the latest in home and business video monitoring, cool gadgets for the home, cloud and mobile technologies, web development, along with other random topics.
jQuery Form Validation Tutorial
- Font size: Larger Smaller
- 21 Comments
There are lots of ways to perform validation on your html forms but nothing is easier than using jQuery’s validation plug-in. I’ll take you through a small tutorial to help you understand how to make it work. Let’s take a look at what you can do and how you can customize the built in validation.
Of course as always make sure you reference the main site here along with all the available validation methods found here. There are also a few good tutorials like this one and especially this one, but I wanted to take the layman's approach right from the beginning. Let’s go into details of what is needed using a simple HTML form example.
As part of this tutorial I'll take you through these items:
- Use HTML and CSS for jQuery validation
- Use jQuery to define your own validation rules
- Define more rules with custom messages
- Create your own custom jQuery validation method
First let's start with the very simplest form using HTML/CSS:
<input class="submit" type="submit" value="Submit"/>
The key here is the the input field for the name has “minlength” and “required”. This will either trigger an empty name or give you the message if the length of characters is smaller than 4. 
It is easy as that. One line of jQuery code. For more details on the attributes you can use see Raymond’s blog. First thing that jumps out is that the styling is butt ugly. Just add a little color and positioning and you are set.

Okay but how about defining those rules in jQuery for more customization? Simple, add to your validation routine and remove the “minlength”/”required” attributes from the HTML:
<input class="submit" type="submit" value="Submit"/>
Let’s keep on building on this. Let’s add a bunch of new criteria such as minimum/maximum length and even custom messages. Nothing changes with the HTML or libraries so here is the changes to the jQuery code:
You can see that I’ve added a few more criteria along with the custom messages. I’ve tied it all to the name in the form “aname”. For the other fields in your form just add to this depending on the name. I’ve also introduced the format method in the message. This lets us replace the argument given for minlength and maxlength. Handy to know that we don’t have to worry about hard-coding that value if they ever change.
To finish it up, lets add a custom validation routine. Again easy with the plugin. This is where the plugin’s addMethod comes into play.
This is a regex example to test for special characters. Here I’m allowing some special characters but others I’m not. So if you enter “*” or “!” for example, you will get the message. Pretty cool huh?
These are the steps I went through to understand this plugin. I’m certainly not an expert (far from it) so comments are most welcome. I hope this can help you on your way.
Comments
-
Monday, 28 January 2013different error text for different element and addMethod you can find a nice example here
http://www.thewebexpert.in/jquery-vidation-js-tutorial/ -
Thursday, 21 March 2013Thanks, Alen, for a nice and true tutorial.
In comparison, this (http://www.thewebexpert.in/jquery-vidation-js-tutorial/) however is better described here as an *example*, and certainly *not* a tutorial as its claims in its website; unless the website would post it as an example of what a tutorial should be not.








nice