JQuery Validation Plugin: Use Custom Ajax Method

Posted by namtax on Stack Overflow See other posts from Stack Overflow or by namtax
Published on 2010-02-03T12:03:35Z Indexed on 2010/04/04 1:23 UTC
Read the original article Hit count: 668

Hi

Looking for some assistance with the Jquery form validation plugin if possible.

I am validating the email field of my form on blur by making an ajax call to my database, which checks if the text in the email field is currently in the database.

    // Check email validity on Blur
       $('#sEmail').blur(function(){
       // Grab Email From Form
       var itemValue = $('#sEmail').val();
        // Serialise data for ajax processing
        var emailData = {
            sEmail: itemValue
        }
        // Do Ajax Call
         $.getJSON('http://localhost:8501/ems/trunk/www/cfcs/admin_user_service.cfc?method=getAdminUserEmail&returnFormat=json&queryformat=column', emailData, function(data){

                    if (data != false) {
                        var errorMessage = 'This email address has already been  registered';
                    }
                    else {
                        var errorMessage = 'Good'
                    }
                })
    });

What I would like to do, is encorporate this call into the rules of my JQuery Validation Plugin...e.g

    $("#setAdminUser").validate({
      rules:{
             sEmail: {
                  required: function(){
                  // Replicate my on blur ajax email call here
                 }                  
             },
            messages:{
                sEmail: {
                    required: "this email already exists"       

            }
        });

Wondering if there is anyway of achieving this? Many thanks

© Stack Overflow or respective owner

Related posts about jquery-validate

Related posts about jquery-plugins