Writing your own form validation code doesn't have to be a scary or time-consuming process. jQuery's Validation Plugin
makes form validation easy and straightforward.
To get started, all you need to do is link to the plugin files (files are also available for download if you'd prefer to host them locally) along with your standard jQuery library link.
From there, you can start coding. Here's what the validation code might look like if you have a form with 4 different input fields (First Name, Last Name, Email, Password) you'd like to validate:
//we're going to run form validation on the #validate-form element
//specify the validation rules
email: true //email is required AND must be in the form of a valid email address
//specify validation error messages
firstname: "First Name field cannot be blank!",
lastname: "Last Name field cannot be blank!",
required: "Password field cannot be blank!",
minlength: "Your password must be at least 6 characters long"
email: "Please enter a valid email address"
It's important to makes sure that the names of your input fields match your jQuery code, so for example, the HTML for the First Name input field should look something like this:
<input type="text" name="firstname">
Additionally, the id of your form needs to match the id called to validate in the first line of the above jQuery code, so in this case, the form's id would need to be #validate-form
in order for this code to work.
That's all it takes to create some basic form validation in only a few minutes! Read through the jQuery Validation Plugin's documentation
to learn even more about how to utilize this helpful plugin.