Posted Date:04-03-2017

In this Post we will explain how to validate html/php forms using jquery validator.

First we will made a simple validation form using JQuery.

Step 1: Include jQuery And Validation Plugin

Create a new HTML file named index.html and include jQuery before the closing body tag:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

Step 2:Create the HTML Form

we create basic html form

  <form action="" id="example" method="post">
    <label for="firstname">First Name</label>
    <input type="text" name="firstname" id="firstname" placeholder="Karthik"/>
    <label for="lastname">Last Name</label>
    <input type="text" name="lastname" id="lastname" placeholder="N"/>
    <label for="email">Email</label>
    <input type="email" name="email" id="email" placeholder="karthik@pheonixsolutions.com"/>
    <label for="password">Password</label>
    <input type="password" name="password" id="password" placeholder="Password"/>
    <button type="submit">Register</button>
 </form>

Step 3: Create the Validation Rules

Finally, initialize the form validation plugin give you form id and add every input field name into script validation

$("#example").validate({
    // Specify validation rules
    rules: {
      firstname: "required",
      lastname: "required",
      email: {
        required: true,
        email: true
      },
      password: {
        required: true,
        minlength: 5
      }
    },
      messages: {
      firstname: "Please enter your firstname",
      lastname: "Please enter your lastname",
      password: {
        required: "Please provide a password",
        minlength: "Your password must be at least 5 characters long"
      },
      email: "Please enter a valid email address"
    },
    // Make sure the form is submitted to the destination defined
    // in the "action" attribute of the form when valid
    submitHandler: function(form) {
      form.submit();
    }
  });

Conclusion:

if you are following above all the steps you getting this kind of code

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

 	<title>Jquery Validation</title>
 	<script type="text/javascript">
 		// Wait for the DOM to be ready
$(function() {
  // Initialize form validation on the registration form.
  // It has the name attribute "registration"
  $("#example").validate({
    // Specify validation rules
    rules: {
      firstname: "required",
      lastname: "required",
      email: {
        required: true,
        email: true
      },
      password: {
        required: true,
        minlength: 5
      }
    },
      messages: {
      firstname: "Please enter your firstname",
      lastname: "Please enter your lastname",
      password: {
        required: "Please provide a password",
        minlength: "Your password must be at least 5 characters long"
      },
      email: "Please enter a valid email address"
    },
    // Make sure the form is submitted to the destination defined
    // in the "action" attribute of the form when valid
    submitHandler: function(form) {
      form.submit();
    }
  });
});
 	</script>
</head>
<body>
<div class="container">
  <h2>Registration</h2>
  <form action="" id="example" method="post">

    <label for="firstname">First Name</label>
    <input type="text" name="firstname" id="firstname" placeholder="Karthik"/>

    <label for="lastname">Last Name</label>
    <input type="text" name="lastname" id="lastname" placeholder="N"/>

    <label for="email">Email</label>
    <input type="email" name="email" id="email" placeholder="karthik@pheonixsolutions.com"/>

    <label for="password">Password</label>
    <input type="password" name="password" id="password" placeholder="Password"/>

    <button type="submit">Register</button>

  </form>
</div>
</body>
</html>

Leave a Reply