Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 510
raymantle Avatar asked Oct 28 '25 22:10

raymantle


1 Answers

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" }
  }
});
like image 138
Jonathan Avatar answered Oct 30 '25 12:10

Jonathan