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

前端 未结 9 927
小鲜肉
小鲜肉 2020-11-28 03:23

I was wondering how to make it so that I could make a rule where a field is not equal to a value. Like I have a field called \'name\' so I don\'t want \'name\' = \'Your Name

9条回答
  •  失恋的感觉
    2020-11-28 03:50

    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:

    $("form").validate({
      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.

提交回复
热议问题