0

I've written the following simple rule for jQuery Validation:

        jQuery.validator.addMethod("notEqual", function(value, element, param) 
        {
          return this.optional(element) || value != param;
        }, "This field is required");

This works such that I can say:

        $("#form").validate({
                rules: 
                {
                    field1: {notEqual: "Please Select", required: true},
                    field2: {notEqual: "Year", required: true}
                },
                messages: 
                {
                    field1: {
                            notEqual: "This field is required", 
                            required: "This field is required"
                            }
                    field2: {
                            notEqual: "This field is required", 
                            required: "This field is required"
                            }
                },

                submitHandler: function(form) {
                form.submit();
            }
        }); 

However I want to be able to say that any one field is not allowed to be equal to multiple things e.g.

field1: {notEqual: "Please Select" || "Year" || "Some Value", required: true},

But I have tried this syntax as well as a few other ways of arranging the "ÖR" operator and none of them work. Can anyone help? Thanks.

1
  • the values that you don't want to match ( "Please Select", "Year", "Some Value") where are they coming from? Are they values of other form fields or are they fixed text values? Commented Nov 27, 2012 at 10:11

1 Answer 1

1

You can pass a JavaScript array as a parameter to a validation method, as described in the answer to jQuery Validation plugin custom method. Multiple parameters

for instance

field1: {notEqual: ["Please Select", "Year", "Some Value"], required: true},

then have your custom method something like this

 jQuery.validator.addMethod("notEqual", function(value, element, param) 
 {
  var result = value != param[0] && value != param[1] && value != param[2]; 
  return this.optional(element) || result;
 }, "This field is required");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.