I am trying to programatically check the validity of my form the code snippet below gives true on the click of button "#btnSet" even if I enter an invalid value in the email field if I click on it again than it gives false.
$.validator.addMethod(
"regex",
function(value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Please check your input."
);
var validator = $("#myForm").validate({
rules:{
cen:"email",
frm:{email:true,required:true},
sub:{required:true, minLength:3,maxLength:50},
fn:{required:true,minLength:3,maxLength:50},
forCols:{regex:"^[1-9]?[1-9](,[1-9]?[1-9])*$"},
ftrCols:{regex:"^[1-9]?[1-9](,[1-9]?[1-9])*$"}
}
});
$("#btnSet").on("click",(function(validator){
return function(){
alert(validator.valid());
if(!validator.valid()){
validator.showErrors();
return;
}
}
}(validator)));
If I replace the click event handler with this function
$("#btnSet").on("click",function(){
alert($("#myForm").valid());
if(!$("#myForm").valid()){
return;
}
});
It give the error below
a.validator.methods[d] is undefined