jQuery Validation Plugin and notEqualTo Rule

On August 26, 2009, in Javascript, by Anuj Gakhar

I have been using the excellent jQuery Validation plugin in my latest project. It provides all the basic rules that you will need for validating your form. However, I had a form with 2 username fields on it and the requirement was that they should not be equal to each other.  I guess this is not a very common scenario, which is why the “notEqualTo” rule was not included in the original ruleset of the plugin. There is a “equalTo” rule already, though.

However, it was pretty easy to write this rule and I am just posting it here incase someone might need it. In my additional-methods.js file, I just added this piece of code :-

[javascript]

jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
return this.optional(element) || value != $(param).val();
}, "This has to be different…");

[/javascript]

And then in your rules definition, all you have to do is this :-

[javascript highlight=”10,18″]

var validator = $("#signupform").validate({
rules: {
username: {
required: true,
minlength: 6,
maxlength:16,
remote:’checkUserName’,
nowhitespace:true,
alphanumeric:true,
notEqualTo:’#otherusername’
},
messages: {
username: {
required: "Enter a username",
minlength: jQuery.format("Enter at least {0} characters"),
remote: ‘Username not available’,
alphanumeric: ‘Letters, Numbers and Underscores Only.’,
notEqualTo: ‘Profile and Customer Usernames cant be same’
}
});

[/javascript]

Pretty easy to do and a big than you to the great plugin.

Tagged with:  

19 Responses to jQuery Validation Plugin and notEqualTo Rule

  1. Biran Maharjan says:

    I had used the method you had given in (https://www.anujgakhar.com/2009/08/26/jquery-validation-plugin-and-notequalto-rule/). this works only for 2 fields. But in my form there are 8 email fields which should not be same from other. How can i make your code workable for multiple fields.

  2. Anuj Gakhar says:

    I guess you are going to have to pass an array of all the “other” 7 emails to the validator and then compare with each of them. Unless there is a plugin already written for such a thing, I cant see any other way of doing this.

  3. […] Validator to validate if 8 email address fields have a unique value. I have blogged about this before but that was for only 2 fields. Simply checking if one field is not equal to another. But here, we […]

  4. It is very helpful that it reminds me to add more plugins like this 🙂

  5. Fobin says:

    Should this work also in jQuery 1.4.2 and Validator 1.7?

    I’ve tried to put it in use at my site http://www.muistiohjelma.fi but it doesn’t seem to validate this:
    rules: {
    paikkakunta: {
    required: true,
    notEqualTo:’Paikkakunta’,
    },
    }

    Can you use string there like have in this?

  6. Anuj Gakhar says:

    It should be an array so it would be something like
    notEqualTo:[‘Paikkakunta’]

  7. Mike says:

    I couldn’t get this work in the additional-methods.js, so I added this to main jquery.validate.js file

    notequalTo: function(value, element, param) {
    return this.optional(element) || value != $(param).val();
    },

  8. Anuj Gakhar says:

    Maybe your additional-methods.js was cached and was not picking up new changes?

  9. Greg says:

    This worked perfectly. Thanks!

  10. Mykola Rykov says:

    Thank you!
    For jQuery 1.5.2 & jQuery Validate 1.8.0 I use it like:

    jQuery.validator.addMethod(
    “notEqualTo”,
    function (value, element, param) {
    return this.optional(element) || value != param.val();
    },
    “Please specify a different value”
    );

    and:


    rules: {
    oneCity: {
    notEqualTo: $(‘#anotherCityId)’)
    },
    anotherCity: {
    notEqualTo: $(‘#oneCityId’)
    }
    },

  11. Berg says:

    Thanks for the code and the blog entry as well.

  12. Umer says:

    Nice post.. and awesome trick…

  13. Mike R. says:

    Thank you for sharing this, it really worked perfectly at my end

  14. Dave says:

    Thank you for this!

  15. chris says:

    Is there any way to add the firstName and lastName together and use equalTo in order to make sure that the “Signature” field is equal to their input in the first and last name boxes?

  16. nawabpurwebs says:

    […] 1: https://www.anujgakhar.com/2009/08/26/jquery-validation-plugin-and-notequalto-rule/ […]

    • sudarshan says:

      i want to validate the security questions.i used this rule.it is working fine but when i select same security questions in two fields.the error mesg is appended to the third fiels also

  17. Frank Nasso says:

    Modified it for greater than or equal to and it works great. Thank you!!!

Leave a Reply to Fobin Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: