Jquery Validation Onblur Example - Jquery

How to validate a textfield onblur?

Snippet Code


  
Rate this page :
  [ 0 votes]

Jquery validation form is used to validate a form. Here, the validation of a form field is done through onblur. The sample code to validate using onblur is given below.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("#txt1").blur(function(){ if ($("#txt1").val()=='') { alert("Enter Name"); } }); $("#txt2").blur(function(){ if ($("#txt2").val()=='') { alert("Enter College"); } }); $("#txt3").blur(function(){ if ($("#txt3").val()=='') { alert("Enter Department"); } }); $("#btn").click(function(){ if ($("#txt1").val()=='') { alert("Enter name"); } else if ($("#txt2").val()=='') { alert("Enter college"); } else if ($("#txt3").val()=='') { alert("Enter department"); } else{ alert("Thank you!!"); } }); }); </script> <form> <table align='center'> <caption><b>Demo Form</b></caption></br> <tr> <td><b>Name : </b></td><td><input type='text' id='txt1'></td> </tr> <tr> <td><b>College :</b></td><td><input type='text' id='txt2'></td> </tr> <tr> <td><b>Department :</b></td><td><input type='text' id='txt3'></td> </tr> <tr> <td></td> <td><input type='button' value='Submit' id='btn'></td> </tr> </table> </form>

Tags


Ask Questions

Ask Question