July 13, 2012

How to use remote method of jquery validation?

Question by Sachin

This is my validation code

$('#reg_form').validate({
            onkeyup: false,
            errorClass: 'error',
            validClass: 'valid',
            rules: {
                username: {
                    required: true,
                    minlength: 5,
                    //remote: "checkusername.php"
                },
                password: {
                    required: true,
                    minlength: 5,
                },
                confirm_password: {
                    required: true,
                    minlength: 5,
                    //equalTo: "#password",
                },
                secretQuestion: "required",
                secretAnswer: "required",
                emailId: {
                    required: true, 
                    email: true,
                    remote: "checkemail.php"
                },
                termsConditions: "required",                
            },
            messages:{
                username: {
                    required: "Please enter Username",
                    minlength: "Please enter atleast 5 characters",

                },
                password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },
                confirm_password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long",
                    //equalTo: "Please enter the same password as above" 

                },
                secretQuestion: "Please select your question",
                secretAnswer: "Please enter your secret answer",
                emailId: {
                    required: "Please enter a valid email address",
                    remote: "Email is aleardy is exists! please try with onother",
                    },
                termsConditions: "Please accept our Terms and COnditions"
            },
            highlight: function(element) {
                $(element).closest('div').addClass("f_error");
            },
            unhighlight: function(element) {
                $(element).closest('div').removeClass("f_error");
            },
            errorPlacement: function(error, element) {
                $(element).closest('div').append(error);
            }
        });

Answer by Starx

There is very easy explanation of the remote method on the jQuery plugin page.

As per my suggestions to you! to understand the method better, use the method in expanded form.

emailId: {
    required: true, 
    email: true,
    remote: {
        url: "checkmail.php", //the url of the page you are querying
        type: "post", //the method you are going to use to send the data
        data: { //the data you are going to send
           email: function() { 
             return $("#email").val(); //send the value of email
           }
        }
    }
},

But, your code should work the way it is.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!