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

前端 未结 9 867
小鲜肉
小鲜肉 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:36

    Nick's answer fits the bill. I needed to compare two fields on the form and make sure they were not equal. I modified it just a bit.

    jQuery.validator.addMethod("notEqual", function(value, element, param) {
     return this.optional(element) || value != $(param).val();
    }, "This has to be different...");
    
    $("#cform").validate(
    {
        rules: {
            referringsales: { required: false, notEqual: "#salesperson" }
        }
    });
    

    Edited to answer comment:

    If you have more than one set of dropdowns to compare, the method also works with that case.

    jQuery.validator.addMethod("notEqual", function(value, element, param) {
     return this.optional(element) || value != $(param).val();
    }, "This has to be different...");
    
    $("#cform").validate(
    {
        rules: {
            referringsales: { required: false, notEqual: "#salesperson" }
            DropDown2: { required: false, notEqual: "#SecondBase" }
        }
    });
    

    If the question is regarding comparing referringsales against 2 different bases (say #initialContact and #salesperson), then simply add that rule to the list.

    referringsales: { required: false, notEqual: "#salesperson", notEqual: "#initialContact" }
    
    0 讨论(0)
  • 2020-11-28 03:40

    Not sure if you got your answer but:

    return this.optional(element) || value != param;

    won't work if the value is variable.

    It needs to read:

    return this.optional(element) || value != $(param).val();

    Cheers

    0 讨论(0)
  • 2020-11-28 03:45

    Thanks a lot, Nick !!! A little adding: if you have a few fields to validate into validate() function, the syntax should be :

    self.logo.rules(
        'add', {
           notEqual: "Select the Logo"
         }
    );
    
    0 讨论(0)
  • 2020-11-28 03:48

    Taking some great examples here, I wrote another version that works with multiple field to check against

    /*=====================================================*/
    /* Jquery Valiation addMethod*/
    /* Usage:  password: {notEqualTo: ['#lastname', '#firstname', '#email']} */
    /*====================================================*/
    jQuery.validator.addMethod("notEqualTo",
        function (value, element, param) {
            var notEqual = true;
            value = $.trim(value);
            for (i = 0; i < param.length; i++) {
    
                var checkElement = $(param[i]);
                var success = !$.validator.methods.equalTo.call(this, value, element, checkElement);
                // console.log('success', success);
                if(!success)
                    notEqual = success;
            }
    
            return this.optional(element) || notEqual;
        },
        "Please enter a diferent value."
    );
    /*=====================================================*/
    /*=====================================================*/
    

    Usage

        $("form").validate(
            {
                rules: {
                    passwordNewConfirm: {equalTo: "#passwordNew"},
                    passwordNew: { notEqualTo: "#password" },
                    password: { notEqualTo: ['#passwordNew', '#passwordNewConfirm'] }
                },
            });
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 03:55

    If you have just one value where you want it to not be like your question implies, you can check against that pretty easily without any external plugins.

    $('#yourForm').submit(function(e) {
        if ( $('input[name="yourField"]').val()=='Your Name' )
            e.preventDefault();
            alert("Your message here");
        }
    });
    
    0 讨论(0)
提交回复
热议问题