1

I made a custom rule for the jQuery validation plugin and I want it to be applied to a field that is not required. So, when I enter something in the field it should be checked but if it's empty it shouldn't be checked by the custom rule.

If I don't set required to true and check this.optional(element) i get dependency-mismatch.

Is there a way to do this?

1 Answers1

1

You can try this. Initially define the rule for required. And then you can add or remove the rule as follows.

str=$("#theFeildId").text();

if(str==""){ 
   $("#theFeildId").rules("remove", "required");
   $("#theFeildId").removeClass("error");
}
else{
    $("#theFeildId").rules("add", "required");
}
Shrujan Shetty
  • 2,298
  • 3
  • 27
  • 44