Thursday, November 24, 2011

Add custom validation in JQuery

When the user click on submit button,It has to be validated
****************************** CustomValidation.html*****************


<script type="text/javascript" src="jquery-1.6.2.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>

<script>
  jQuery.validator.addMethod("selectNone",
      function(value, element)
       {
         if (element.value == "none")return false;
         else return true;
       },
             "Please select an option."
      ); //addmethod
     
     
      $(document).ready(function() {
                              $("#form2").validate(
                              {
                                rules:
                                {
                                  sport: {"selectNone": true}
                                }
                              }); //validate
                            }); //function
</script>
<body>
        <form id="form2">
                <select name="sport" id="sport">
                    <option value="none">Select a sport</option>
                    <option value="baseball">Baseball</option>
                        <option value="basketball">Basketball</option>
                    <option value="soccer">Soccer</option>
                    <option value="football">Football</option>
                </select> <br>
            <input class="submit" type="submit" value="Submit"></div>
        </form>
</body>
***********************ProgramEnds********************************************
Collect JavaScript files Separately

No comments:

Post a Comment