Validate forms in client side using jQuery

There is a quick way for validating forms if you are using jQuery.

1. Download the validation plugin

You need to download the jQuery Validate plugin. Alternatively you can make a call to the online version from your code.

2. Add the plugin to your website

I presume you are yet using the latest jQuery library and you don’t need any explanation on how to use it, then, just add another reference to the plugin in your code so you can use it:




3. Make a call to validation

Now, if you just want to validate a form using default validation you only need to make a call to this function:


$(document).ready(function () {
$('form').validate();
});


You can of course have more forms and select the one you want to validate etc. But lets simplify it for this example. Now, the form will be validated but since the rules are not set jQuery will not know what to validate.

4. Adding rules to validation

To add rules you only need to add style classes to the class atribute like this:

Name
Website
Phone
Email

As you can see, Ive add some classes and just doing that the form will be validated and the input fields be checked with the conditions Ive added through the classes. You can add multiple conditions to make a field required and number, for example.

Here is a list of some of the class styles you can use:

required

email

number (decimal)

digits (integer)

date

dateISO

url

creditcard

You can also add some properties like minlength, maxlength or range:

Name
Website
Phone
Email

And that will help where adding classes wouldn’t work (you need to set a value). A list of them:

minlength

maxlength

rangelength

min

max

range

accept (file extensions)

5. Customizing validation

But despite thats so useful, quick and flexible you may need more specific conditions or just want to add your own error messages. For doing so its still very easy when you make the call to the validate function, just adding custom values:

$('form').validate({
rules: {
txtName: required,
website: {required: true, minlength: 5},
phone: {required:true,
digits: true,
rangelength: [6, 9]
},
email {required: true}
}, messages: {
txtName: required : 'The name is required',
txtSurname: { required: 'The website is required',
minlength: 'The website need to be at least 5 characters length'}
phone { rangelength: 'The phone needs to be between 6 and 9 characters' }
}
});

You can add individual conditions without brakets like in name, or add more conditions using brackets. To make them match the fields you have to add the name property to them:

Name
Website
Phone
Email

6. Customizing messages style

Error messages will show a label like this one:



You can add a style in your stylespage or in the same page to change its look and make it fit your page better. Notice you can use the class .error, but also the property generated=”true” or the property “for” so that you can specify more properly in your css:

.error{} /*general error messages*/
.error[generated=true] {} /*generated error msgs when jQuery validates */
.error[for=txtName]{} /*very custom style for error msg when txtName is not correct*/

You can check more options, methods, etc. at its documentation page.