3

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

4 Answers 4

13

You are doing an AJAX request, ergo: the validation is already finished working when your custom validator returns either true or false.

You will need to work with async. See also this post: How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

Lets say we have a custom validation to check the Unique Mobile, So we will add a new method as follows to check the unqiue Mobile no :

jQuery.validator.addMethod("uniqueMobile", function(value, element) {
    var response;
    $.ajax({
        type: "POST",
        url: <YOUR SERVER SCRIPT/METHOD>,
        data:"mobile="+value,
        async:false,
        success:function(data){
            response = data;
        }
    });
    if(response == 0)
    {
        return true;
    }
    else if(response == 1)
    {
        return false;
    }
    else if(response == 'logout')
    {
        return false;
    }
}, "Mobile is Already Taken");
Sign up to request clarification or add additional context in comments.

4 Comments

can we use $.get() for this ?
yes you can use $.get() function as it is a abstract method to make a ajax call to the server through jQuery, but in that case you have to send your data for eg: "mobile="+value appended to the url you are requesting.
async: false, will lock the interface
I am Getting an error response is not defined
1

You can add a custom validation method like this

 $.validator.addMethod('unique',
                     function(value) {
        $.get('http://localhost:8501/ems/trunk/www/cfcs/admin_user_service.cfc','?method=getAdminUserEmail&returnFormat=json&queryformat=column&emailData='+ value,
                            function(return_data) {
                                return return_data;
                            })
                         }
                         , "this email already exists"
                    );

then add the unique class to your email field or you can use something like this

$.validator.addClassRules("unique-email", {
                unique: true
            });

Comments

1

Try this:

$.validator.addMethod('unique', 
            function(value) {
         var result = $.ajax({ 
                             async:false, 
                             url:"http://yoursamedomain.com/checkemail",
                             data:'email='+ value, 
                             dataType:"text" }); 

         if(result .responseText) return true; else return false;

             } , "This email address has already been  registered");

1 Comment

Hi, i really appreciate your response, I have moved on from this project so cannot apply your feedback. Im sure it will come in handy however, next time I am using jQuery. Thanks
0

For serverside AJAX validation use remote rule. This is same as standard jQuery AJAX request object. Your server must returns string "true" for valid value or "false" for invalid, also it can passes some string "This email was already taken", and this will be perceived as invalid, and render as an error message:

$( "#myform" ).validate({
  rules: {
    email: {
      required: true,
      email: true,
      remote: {
        url: "check-email.php",
        type: "post",
        data: {
          username: function() {
            return $( "#username" ).val();
          }
        }
      }
    }
  }
});

Read documentation here remote-method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.