How to validate a form using jQuery?

At first you have to make a html form like.

<h2>Validation of a form</h2>
<form id="form" method="post" action="">
First name:<br>
<input type="text" name="firstname" value="john">
Last name:<br>
<input type="text" name="lastname" value="Doe">
<input type="email" name="u_email" value="">
<input type="submit" value="Submit">

Now use jQuery validation plugin to validate forms' data in easier way.

first,add jQuery library in your file.

<script src=""></script>
then add javascript code:
$(document).ready(function() {

Then to define rules use simple syntax.

jQuery(document).ready(function() {
      rules: {
         firstname: 'required',
         lastname: 'required',
         u_email: {
            required: true,
            email: true,//add an email rule that will ensure the value entered is valid email id.
            maxlength: 255,

To define error messages.

messages: {
   firstname: 'This field is required',
   lastname: 'This field is required',
   u_email: 'Enter a valid email',

Now finally to submit the form.

submitHandler: function(form) {

Updated on: 09-Sep-2023

27K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started