raymantle raymantle - 1 month ago 10
jQuery Question

jQuery validator notEqual to multiple values

I have a text input called strCompanyName which I need to perform some validation on.
Now I have the below working fine where I get a warning if the text in the input matches "Company", however, I am unsure how I would check to see if the input matches a number of values ie. "Company", "Your Company", "Your Company Name"

jQuery.validator.addMethod("notEqual", function(value, element, param) {
return this.optional(element) || value.toLowerCase() != param.toLowerCase();
}, "Please specify your company name");


$("form").validate({
rules: {
strCompanyName: { notEqual: "Company" }
}
});


Any help would be greatful.

Answer

Change your custom validator to split on a symbol (say, pipe) and validate against the subsequent array:

http://jsfiddle.net/sw87W/40/

jQuery.validator.addMethod("notEqual", function(value, element, param) {
return this.optional(element) || param.toLowerCase().split('|').indexOf(value.toLowerCase()) < 0;
}, "Please specify your company name");

and

$("form").validate({
  rules: {
    strCompanyName: { notEqual: "Company|Another Company|Etc" }
  }
});