0
function checkEmailValid(email,result)
    {
        if(email.val()==""){
            result.text("이메일을 입력하십시요.");
            return false;
        }
        else{
            var re_mail = /^([\w\.-]+)@([a-z\d\.-]+)\.([a-z\.]{2,6})$/;
            if(re_mail.test(email.val())){
                var res = false;
                $.ajax({
                    type:"GET",
                    url: "http://127.0.0.1:3000/auth/checkemail/"+email.val(),
                    timeout: 2000,
                    beforeSend: function(){
                        result.text("이메일 확인 중...");
                    },
                    complete: function(jqXHR){
                        if(jqXHR.status==202){
                            res = true;

                        }else{
                            result.text("이미 등록된 이메일입니다.");
                            res = false;    
                        }
                    },
                    success:function(data,textStatus,jqXHR){

                    },
                    fail: function(){

                    }                       
                });
                alert("step2 : " + res); ////////Here is Step2!!!
                alert("step3 : " + res); ///////Here is Step3!!!
                return res;

            }else{
                result.text("이메일 형식이 올바르지 않습니다.");
                return false;   
            }
        }
    }

Above function is for Email Validation:

If the email is valid the function will return true, but oddly enough res of step2 is false and res of step3 is true. What is happening?

Step 2 value:

enter image description here

Step 3 value:

enter image description here

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Aram_Yook
  • 21
  • 1

1 Answers1

0

i will not suggest you for setting "async" to false because it is a bad practice but you can use callback function to perform your task on true condition

i will explain

first of all add another parameter in your function like

function checkEmailValid(email,result,SuccessCallback)

and create a function with which will perform your task when ajax return true

like

function EmailIdIsUnique()
{
    //perform your code here
}

and call this callback funtion in ajax

like

complete: function(jqXHR){
                        if(jqXHR.status==202){
                         //run your code here 
                          if ($.isFunction(SuccessCallback))
                          {
                            result = successcallback.call(this);                        
                          }
                        }else{
                            result.text("이미 등록된 이메일입니다.");
                            res = false;    
                        }
                    },
Virendra Yadav
  • 652
  • 8
  • 18