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 :-


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

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


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'
 }
 });

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