How to add a Not Equal To rule in jQuery.validation

You could use a custom method, something like this:

jQuery.validator.addMethod("notEqual", function(value, element, param) {
  return this.optional(element) || value != param;
}, "Please specify a different (non-default) value");

Then use it like this:

  rules: {
    nameField: { notEqual: "Your Name" }

Adding it as a rule like this makes it more extensible, so you can use it to compare against the default value in other fields.

Leave a Comment