S.aad
S.aad

Reputation: 538

jQuery Validation - .valid method first attempt fails to validate

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

Upvotes: 0

Views: 71

Answers (1)

Sparky
Sparky

Reputation: 98718

a.validator.methods[d] is undefined means that you have undefined methods (rules); in other words rules that are invalid...

sub:{required:true, minLength:3,maxLength:50},

In this case, your rules are not properly spelled. It's NOT minLength and maxLength, but minlength and maxlength...

....
sub: {
    required: true, 
    minlength: 3,
    maxlength: 50
},
....

Upvotes: 1

Related Questions