Ketan patil
Ketan patil

Reputation: 95

return true/false is not working in ajax code

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;
    }
}

});

Upvotes: 0

Views: 377

Answers (1)

Sasidhar Vanga
Sasidhar Vanga

Reputation: 3404

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.

Upvotes: 1

Related Questions