Hi everybody. Sometimes you may find yourself working on projects that require some intelligent form field validation..but what’s the easiest way to achieve this? I’ve used everything from the popular Validate plugin to Spry components, but I found all of them a little too much to achieve something so simple. In this post I’m going to show you how to add sleek form validation to your input fields in just two easy steps.
First you’ll need to download my tutorial pack for this post. It’s composed of a small jQuery Plugin we’ll be using for this tutorial which you can include with jQuery as follows:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="validation.js"></script>
Lets take a look at the HTML.
HTML >> demo.html
To keep things simple our jQuery Plugin uses a template structure for each input field you would like to validate. For this post, I’m going to use the "username" field as an example of how to use it.
1. Here is our input field template you can use for anything like usernames, passwords, addresses and so forth. Notice that we have an image area to display a notification icon and a message area to display any validation messages.
<li class="validated" id="FIELDNAME_li">
<label for="r_FIELDNAME">Fieldname:</label>
<div id="FIELDNAME_img"></div>
<input class="validated" name="FIELDNAME" id="FIELDNAME" type="text" maxlength="20" value="" />
<div id="FIELDNAME_msg"></div>
</li>
Lets now replace FIELDNAME with username to setup an input field that validates.
<li class="validated" id="username_li">
<label for="r_username">Username:</label>
<div id="username_img"></div>
<input class="validated" name="username" id="username" type="text" maxlength="20" value="" />
<div id="username_msg"></div>
</li>
2. The next stage of setting up a new text field for validation is adding our "username" field to our JavaScript. This takes the form of a check to see if the name of one of the input fields on the page is "username" and if so, what type of validation we would like to apply on it.
JavaScript >> Validator.js
$.fn.validate = {
init: function(o) {
//Here’s where I’m checking for my input field name
if(o.name == ‘username’) { this.username(o) };
if(o.name == ‘password’) { this.password(o) };
if(o.name == ‘email’) { this.email(o) };
if(o.name == ‘dob’) { this.dob(o) };
},
//and here’s where I’m definining a validation method for it
//note the regex used to make sure that our username fits the
//expected pattern
username: function(o) {
var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\’\"\`\~\\#\$\%\^\&\<\>)+]/;
if (!o.value.match(user)) {
doValidate(o);
} else {
doError(o,’no special characters allowed’);
};
}
(each of the password, email, date-of-birth items also have their own custom validation scheme which you can see in the tutorial pack)
And that’s it!. You can define as many input fields as you would like and validate their content using any type of regular expression that your project requires the data to conform to.
If you would like to try the methods described in this post out for your own project, feel free to download my tutorial pack here or view a demo here.
ShareRelated posts:
Related posts brought to you by Yet Another Related Posts Plugin.
RSS feed for comments on this post. TrackBack URL
September 30th, 2009 at 10:41 am
And where found validation.js?????
September 30th, 2009 at 10:32 am
[...] This post was mentioned on Twitter by Adnan Osmani and jQuery Tips. jQuery Tips said: How To Add Form Validation using jQuery in 2 Easy Steps – http://bit.ly/Dgvj9 #jQuery [...]
September 30th, 2009 at 3:40 pm
Thank you very much!
it's ok to validate some of the input date in the client/browser side, but it is more important to validate it in the server side.you know it's easy to create a GET/POST request pointing to the cgi or script.
September 30th, 2009 at 3:41 pm
i meant "data" not date, sorry.
September 30th, 2009 at 3:45 pm
Hi Bob. You can find it in the tutorial download pack.
September 30th, 2009 at 3:22 pm
[...] here: How To Add Form Validation using jQuery in 2 Easy Steps … By admin | category: cgi script | tags: browser-side, client, more-important, [...]
October 2nd, 2009 at 3:47 am
I like this but surely you would want to allow some special characters for the password field! some users prefer to use special characters to strengthen their password!
Then you can trim the field or use escape string on the server side.
October 2nd, 2009 at 3:54 am
Hi Tommy and thanks for the comment. The beauty of this method's Regex support is that you can define a set of Regular expression rules to accept any type of password you would like. For example, if you did want to define a password regex that the field had to validate against you could easily have it report back to the UI prompting the user to include some special characters otherwise their password won't be accepted :)
October 2nd, 2009 at 3:57 am
Hi David,
I think that it's just as important to validate what your users enter on the client side as it is on the server side – if you can ensure that the login data being sent back to the server is (or is as close to) what's expected rule-wise then your server can simply get onto the task of adding the new user to the database as opposed to refusing the form back and forth until the user gets it right. Focusing on getting it right on the client side helps to remove a small amount of added communication made to the server (which is why I'm a supporter of developing for both in mind) :)
October 23rd, 2009 at 8:27 am
It was simply that he was not prepared to pre-empt the report which was about to be published. ,