0

I want to validate email, what's wrong in this. Return false is not working

$.validator.addMethod("email_exist", function(value, element, arg){
    var x = 0;
    $.ajax({
        url: '../signup/validateemail.php',
        type: 'post',
        data: 'email_address='+value,
        cache:false,
        success: function(response) { 
            if(response == 0){
                x++;
            }
        }
    });
    alert(x);
    if(x > 0){
        return false;
    }else{
        alert('yeh');
        return true;
    }
    return true;
 },'');
SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62

2 Answers2

1

You do asynk call.So in you case,first execute alert(x),then code in success function. You can do this request synk (async: false) or change code in success

vborutenko
  • 4,323
  • 5
  • 28
  • 48
1

it wont return false as the ajax method is asynchronous, it will not wait for the success event to happen. You can make the ajax synchronous by making

async: false

but it is not a good method. It will freeze the browser as well

Refer this

Community
  • 1
  • 1
Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53