Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery form validate plugin: field 1 not equal to field 2

heyho,

i stuck in the following problem: i try to prevent that a user can choose for the fields "username" and "email" the same values. i'm using the jquery form validate plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/)

would be awesome if somebody could show me the way, please see my code here:

// validate form    
$("#signupform").validate({

    rules: {
        username: {
            required: true,
            minlength: 2,
            remote: "/_misc/formcheck/username.cfm"
        },
        password1: {
            required: true,
            minlength: 5
        },
        password2: {
            required: true,
            minlength: 5,
            equalTo: "#password1"
        },
        email: {
            required: true,
            email: true,
            remote: "/_misc/formcheck/email.cfm"
        },
        email2: {
            required: true,
            email: true,
            equalTo: "#email"
        },
        usage: "required"                       
    },
    messages: {
        username: {
            required: "please username!!",
            minlength: "username at least 2 chars"
        },
        password1: {
            required: "please password!",
            minlength: "password at least 5 chars"
        },
        password2: {
            required: "please password",
            minlength: "password at least 5 chars",
            equalTo: "password fields have to match"
        },
        email: "validate email please!",            
        email2: {
            required: "please provide correct email address!",              
            equalTo: "email fields must match!"
        },
        usage: "please check my terms!" 
    }, 

thanks in advance, michbeck

like image 414
michbeck Avatar asked Oct 26 '11 15:10

michbeck


1 Answers

You can add a custom method checking the user name and email values and add validation for that field.

Demo - http://jsfiddle.net/2hA8M/8/

Example -

Method -

$.validator.addMethod("user_email_not_same", function(value, element) {
   return $('#name').val() != $('#email').val()
}, "* User and Email should not match");

Validation -

rules : {
     username : {
         user_email_not_same: true
     }
 },
like image 176
Jayendra Avatar answered Oct 11 '22 03:10

Jayendra