0

I am checking duplicate email from database. first i checked valid email then checked email length then i checked email from database using ajax. so return true or return false is not working in ajax code.other validation is ok but checking duplicate email is not working. also ajax response is ok.
here my js file -

$(document).ready(function(){

var form = $("#form");
var registeremail = $("#registeremail");
var emailInfo = $("#emailInfo");

registeremail.blur(validateEmail);
registeremail.keyup(validateEmail);


form.submit(function(){
    if(  validateEmail() )
        return true;
    else
        return false;

});


function validateEmail(){
    //testing regular expression
    var a = $("#registeremail").val();
    var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
    //if it's valid email

    if(filter.test(a)){
        registeremail.removeClass("error");
         emailInfo.text("");
        emailInfo.removeClass("error");
        if(registeremail.val().length < 8){
            registeremail.addClass("error");
            emailInfo.text("Min 8 letters required.");
            emailInfo.addClass("error");
            return false;
        }
        else if(registeremail.val().length > 60){
            registeremail.addClass("error");
            emailInfo.text("Max 60 letters required.");
            emailInfo.addClass("error");
            return false;
        }
        else
            { 
            $.ajax({
                  url: "home/CheckEmail",
                  data: {
                      email: a
                  },
                  success: function(ret)
                  {
                    if(ret=='1')
                    {
                        registeremail.addClass("error");
                        emailInfo.text("Email already exist.");
                        emailInfo.addClass("error");
                        return false;
                    }
                    else if(ret != '1')
                    {
                          registeremail.removeClass("error");
                          emailInfo.text("");
                          emailInfo.removeClass("error");

                          //document.forms["form"].submit();
                         return true; 
                    }
                  }
            });
            }
    }
    //if it's NOT valid
    else{
        registeremail.addClass("error");
        emailInfo.text("Valid e-mail please");
        emailInfo.addClass("error");
        return false;
    }
}

});

2
  • 1
    $.ajax returns jQuery, not the return of the success or failure callbacks that you pass to it. Your best bet is to perform all logic in the callback, or trigger an external function and passing the return value as a param. Commented Mar 8, 2013 at 6:35
  • $.ajax is asynchronous. You need to use the option async : false to use your script like this : api.jquery.com/jQuery.ajax Commented Mar 8, 2013 at 7:03

1 Answer 1

1

By default, jQuery's ajax call will run asynchronously.

So, When you do an ajax call, the success or error callbacks doesn't run immediately.

To make the above code workable, you can add 'async : false' to the ajax call parameters.

But making synchronous ajax calls will freeze the UI till it's completion. If the back-end api call takes much time to complete then this will become a show-stopper.

Ideal way is

  1. Show some processing/loading icon
  2. Send AJAX call
  3. After getting response, hide processing/loading icon
  4. Based on response, show error/success messages.
Sign up to request clarification or add additional context in comments.

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.