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.
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.
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.
[…] 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 […]
I have written up a blog entry for this. Hope this helps.
https://www.anujgakhar.com/2010/05/24/jquery-validator-plugin-and-notequalto-rule-with-multiple-fields/
It is very helpful that it reminds me to add more plugins like this 🙂
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?
It should be an array so it would be something like
notEqualTo:[‘Paikkakunta’]
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();
},
Maybe your additional-methods.js was cached and was not picking up new changes?
This worked perfectly. Thanks!
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’)
}
},
…
Thanks for the code and the blog entry as well.
Nice post.. and awesome trick…
Thank you for sharing this, it really worked perfectly at my end
Thank you for this!
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?
[…] 1: https://www.anujgakhar.com/2009/08/26/jquery-validation-plugin-and-notequalto-rule/ […]
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
Modified it for greater than or equal to and it works great. Thank you!!!