Wednesday, December 25, 2013

How to validate simple html form using jquery validate plugin

In this example, I have created a simple form which has user name and password as input fields.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
// Validate plugin which I got from http://plugins.jquery.com/validation/
<script src="jquery.validate.js"></script>
</head>

<body>
<script>
$(function() {
 $("#form1").validate({
                // Use this one to place the error
  errorLabelContainer: $("#form1 div.errorHolder")
 });
});
</script>
<form id="form1" action="">
  <p>
   <label>Username</label>
   <input name="user" title="Please enter your username " required minlength="5">
  </p>
  <p>
   <label>Password</label>
   <input type="password" maxlength="12" name="password" title="Please enter your password" required minlength="5">
  </p>
                 // Div tag to hold error messages
  <div class="errorHolder">
  </div>

  
   <input class="submit" type="submit" value="SUBMIT"/>
  
</form>
</body>
</html>



Simple sign in form example

You can copy the above code and save it in a file .html to test it.



No comments:

Post a Comment