Quick & Easy Form Validation with JQuery
Form validation has become never been easy like before. If you havent heard of jQuery, then its time to get know now. Jquery is an open source set of javascript library, that brings user interactivity in web applications so easy even for a beginner with just few lines of code.
In this tutorial, i am going to show how easy to validate a form with jquery. For this you will need to download
JQuery Library – http://jquery.com
JQuery form validation plugin – http://bassistance.de/jquery-plugins/jquery-plugin-validation/
Lets say we are going to validate a registration form like shown below..
Step 1:
Place the javascript files within the head section
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script>
$(document).ready(function(){
$("#registerForm").validate();
});
</script>
</head>
You should set the name and id of your html form same as $(“#registerForm”).validate();
Step 2:
For each required form element just place class=”required” and specify minlength tag inside it. For example
Name field -> class=”required”
Email field -> class = “required email”
<input name="name" type="text" id="name" class="required"> <input name="user_id" type="text" id="user_id" minlength="4"> <input name="user_email" type="text" id="user_email" class="required email">
Thats it! you will be able to validate form field textboxes without any advanced code.
For optional fields, you can specify class=”optional” or just leave it.
Step 3
In this last step you will add some styles to the error messages in the stylesheet.
.error {
font: normal 10px arial;
padding: 3px;
margin: 3px;
background-color: #ffc;
border: 1px solid #c00;
}
Thats it! see how the validation works like a charm..
Advanced Validation
The following are some of bit advanced validation code that might help you.
To check password and password retype are same
<tr>
<td>Password</td>
<td><input name="pwd" type="text" id="pwd" class="required" minlength="5"></td>
</tr>
<tr>
<td>Retype Password</td>
<td><input name="pwdc" type="text" id="pwdc" class="required" equalTo="#pwd"></td>
</tr>
To validate a website URL – optional field
It should check the URL and force the user to enter full url with http.
<tr>
<td>Website </td>
<td><input name="web" type="text" id="web2" class="optional defaultInvalid url">
<span class="example">http://www.example.com</span></td>
</tr>
To validate Phone Number
We dont want restrict phone numbers to be US only. Suppose if a foreign person enters a phone with + and dashes, we want that as well. To do this you will need to make slight change inside document.ready() in head section.
<script>
$(document).ready(function(){
$.validator.addMethod("NumbersOnly", function(value, element) {
return this.optional(element) || /^[0-9\-\+]+$/i.test(value);
}, "Phone must contain only numbers, + and -.");
$("#regForm").validate();
});
</script>
and now you can validate specifying class=”required NumbersOnly”. This will allow only numbers 0-9, hyphen and + for country code.
<tr>
<td>Phone</td>
<td><input name="phone" type="text" id="phone" class="required NumbersOnly"></td>
</tr>
Validating Username with no special characters
We dont want username to contain any special characters like dot,slashes etc. We only want to allow alphabets, numbers and underscore.
We create a function similar way like above
<script>
$(document).ready(function(){
$.validator.addMethod("username", function(value, element) {
return this.optional(element) || /^[a-z0-9\_]+$/i.test(value);
}, "Username must contain only letters, numbers, or underscore.");
$("#regForm").validate();
});
</script>
and now you can call using class=”required username”
<tr> <td>User ID</td> <td> <input name="user_name" type="text" id="user_name" class="required username" minlength="4"> </td> </tr>
Enjoy jQuery!
Similar Posts:
- how to strip special characters with jQuery form validation!
- Toll Free US Phone and Fax Nos
- How URL shortening scripts work?
- Affiliate Hide – Free wordpress plugin to redirect affiliate links!
- PHP – How to get domain name from URL?
- Tutorial: How to write a Wordpress Plugin?
- Fix-> JQuery onclick browser jumps to top of page!
- Best Mobile Phone for Senior Citizens
- A Simple WYSIWYG Editor in Javascript
- how to store and retrieve checkbox value in php?


August 6, 2009
WOW THANKS ALOT!
February 15, 2010
nice
September 10, 2009
its fantastic…but i expected more advanced jquery structures with good examples
September 11, 2009
Is there like message key properties in jquery validation? For internationalization. Like in your example you have a message which is “Username must contain only letters, numbers, or underscore.” , when browser locale becomes zh_CN it automatically changes to chinese. It’s easier if jquery can render this .html(‘
September 11, 2009
Is there a way where I can put the validation next to the label list instead of the bottom of the text box?
December 1, 2009
thanks for the good tutorial .. but it’s complex a bit for me !
January 28, 2010
wow.. thx for this tutorial.. make help me
February 10, 2010
Wow……! Great example……..Tanks alot…. It save me from a big problem….. Tanks once again……..
February 10, 2010
great tutorial maan…..
this saved us a lot of time that we used to spent for validation purposes in our projects..
thanx oce again for this simple and elegant style.
March 14, 2010
Nice tutorial
March 15, 2010
Clearly, if you know for sure that you’re not going to live very long, an annuity is a poor choice. But what if you guys keep going and going? Will your IRA be able to support you for, say, 25 years? How about 30 or 35 years?