In the following code, the ajax call works and isTaken
is updated properly. But return isTaken
executes before the ajax call completes so it always returns the initial false
setting. async = true
is deprecated so how do I wait for the ajax call to finish before returning a properly updated isTaken
response?
$.validator.addMethod("checkExists",
function (value, element) {
var email = $("#email").val();
var data = {email: email};
var isTaken = false;
$.ajax({
type: "POST",
url: "http://localhost/CFBserver/check_email.php",
data: data,
success: function (response) {
isTaken = (response == 1) ? true : false;
}
});
return isTaken;
},
"This username is already taken! Try another."
);