Form Validation Using Jquery Examples

When you include forms on your website for user input, it is always advisable to perform some validation on the data entered. This not only ensures that your application receives the type of data it can work with, but it also makes for good UX (user experience) as users will be able to be warned of the incorrect input and be given hints of the kind of data that is acceptable.

There are two kinds of validation: client-side and server-side validation. It is recommended that you perform both. Client side validation is usually done with JavaScript and it serves to give the user quick feedback on input errors before a call to the server is made with the data. Server side validation serves as a second line of defence against invalid data. Even if you have set up client side validation, you should always perform server side validation as JavaScript can be bypassed.

In this tutorial, we’ll look at how client side validation can be done using jQuery. We’ll see two ways of doing this. First we’ll validate a form’s data by writing the validation rules from scratch and then in the second example, we’ll show how this process can be made easier by using the jQuery Validation Plugin.

If you want to see how you can validate your form quickly usingthe jQuery Form validation plugging, head straight to The Most Indispensable jQuery Form Validation Reference Guide

To get started, include jQuery in your HTML file.

Add the following form to the file.

The above markup is for a simple form with four fields and a submit button.

Add the following CSS styles.

The resulting form can be seen below.

HTML Form

Add the following JavaScript code to the file.

In the above code, we set a listener on the form that will be called when it is submitted. In the listener, we first make a call to preventDefault() to prevent submission of the form’s data. We then create some variables and assign them the values of the different fields found in the form.

Ignore the $(".error").remove(); statement for now, we’ll get to it later.

After getting the form’s field values, we run some tests on them to check whether they are valid or not. We check to see whether the first_name, last_name and email fields have input. If the user left a field blank, a span tag will be appended after the field with an error message.

We add another check for the email field ensuring that the value entered is a valid email address format. We use a regular expression for this check.

Finally we check the password length to ensure that it’s at least 8 characters long.

Before the code that validates the fields, you’ll find the statement $(".error").remove();. This removes any element with the error class from the documents. This ensures that on subsequent submissions, the form will not have the previous error messages. If we didn’t include this, then every time the form was submitted with errors, an error message would get appended on the previous error message, resulting in the form displaying multiple error messages.

Try submitting the form with invalid data and you should see the error messages.

HTML Form

Using the jQuery Validation Plugin

In the previous example, we wrote code that validated the forms data from scratch. To make this process faster and easier, you can use a library like the jQuery Validation Plugin. With this, you only have to specify a few rules for each field of your form that is to be validated, and the plugin will take care of validation for you.

To see this in action, first include the library in your file. Don’t remove the jQuery script tag as the plugin depends on jQuery.

Add another form to the file.

And add the following JavaScript code inside the $(document).ready(function() {}) block, after the $('#first_form').submit(function(e) {}) block.

We add the required rule to all fields of the form. For the email field, we add an email rule that will ensure the value entered is a valid email. The password field has an added rule that ensures its minimum length is 8 characters.

After specifying the rules, we set the error messages that will be shown whenever a rule fails.

Launch the page and test the form and you should still be able to get validation on the form fields.

Demo

light

Comments on this entry are closed.