Skip to content Skip to sidebar Skip to footer

How To Create A Universal Jquery Validate And Reset Function Call

I am using the jquery validate plugin to create a validation method for all forms in an application. Using the jquery each() function to cycle each form and applyt the method. I wa

Solution 1:

Without the var keyword, you're setting validator as a global variable, and changing what it's set to with each loop (so it'll be set the to the validator of the last form at the end), just add var when declaring it, like this:

$("form").each(function () { 
   var validator = $(this).validate();  //add var here
   validator.resetForm();
   $("#reset").click(function () {
       validator.resetForm();
   });
});

This will correct it so you'll reset each <form> once, rater than the last <form>n times.

Solution 2:

try this maybe?

$("form").each(function () { 
var validator = $(this);
validator.validate(); 
validator.resetForm(); 
$("#reset").click(function () { 
    validator.resetForm(); 
}); 
 });

Post a Comment for "How To Create A Universal Jquery Validate And Reset Function Call"