69

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.'

Does anybody have an idea of how to do this? thanks for any help.

Kyle Hotchkiss
  • 10,754
  • 20
  • 56
  • 82

9 Answers9

130

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.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
37

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" }
Frank Luke
  • 1,342
  • 3
  • 18
  • 33
  • Sam's comment: "@Frank Luke : what if I have 2 different dropdowns?" – Peter O. Mar 02 '12 at 07:42
  • @PeterO., the second drop down would have its own rule. Like this: secondDropDown: { required: false, notEqual: "#secondBase" } The second base can be the first one or a different one. The notEqual method checks the one referred to. Or do you mean you have to compare one value to 2 different drop downs? – Frank Luke Mar 02 '12 at 14:49
  • 1
    That's not my comment. I merely added it here because its author wrote it as an answer rather than a comment. For that reason you should modify this answer to respond to the comment. – Peter O. Mar 02 '12 at 14:52
14

I propose a multi-valued function...

jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
    var notEqual = true;
    value = $.trim(value);

    for (i = 0; i < param.length; i++) {
        if (value == $.trim($(param[i]).val())) { 
            notEqual = false; 
        }
    }

    return this.optional(element) || notEqual;
}, "Please enter a diferent value.");

And I call it...

$("#abm-form").validate({
    debug: true,
    rules: {
        password1: {
            required: true,
            minlength: 10,
            notEqualTo: ['#lastname', '#firstname', '#email']
        },
        password2: {
            equalTo: '#password1'
        }
    }
});

This works for me!

Mr.Singh
  • 1,421
  • 6
  • 21
  • 46
Jose
  • 141
  • 1
  • 2
9

There's one called "notEqualTo" in additional-methods.js in the download package:

https://github.com/jzaefferer/jquery-validation/blob/master/src/additional/notEqualTo.js

TrueWill
  • 25,132
  • 10
  • 101
  • 150
3

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'] }
    },
});
Mr.Singh
  • 1,421
  • 6
  • 21
  • 46
Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
2

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

Richard L
  • 21
  • 5
1

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");
    }
});
Robert
  • 21,110
  • 9
  • 55
  • 65
  • yes but the question is "How to add a Not Equal To rule in jQuery.validation" you may reply to this. this is a different case – Alp Altunel Sep 17 '18 at 13:08
0

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"
     }
);
Sasha.M
  • 77
  • 1
  • 7
0

Thanks for @Nick Craver♦'s answer, but in my working environment, it needs to be slightly modified before it could work.

 $.validator.addMethod("notEqualTo", function(value, element, param) {
       return this.optional(element) || value != $(param).val();
 }, 'This two elements are the same, please change it.');
Xiao Ai
  • 1
  • 1