0

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

1
  • Please properly format your code for readability. Commented Apr 12, 2017 at 15:44

1 Answer 1

1

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
},
....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.