I'm fairly new to javascript and AJAX and I can't figure out why this isn't working.
What I'm trying to accomplish here is email validation using prototype's Ajax.Request method and ASP.net. The problem is that when I call the function (
validateEmail('element')) it returns an undefined value. Within the validateEmail function I have a boolean called bValid. When the function is called it makes an ajax request to validate the email address and then assign true or false to bValid then return bValid, but bValid doesn't seem to want to retain a value. I know that the request is working and returning correct values it's just bValid (that is outside the request function) that's being difficult.
Below is the code I have. Any help would be greatly appreciated.
Code:
function valEmail(elementID) {
var bValid;
new Ajax.Request('../apps/valEmail.aspx', {
method:'get',
parameters: {email: $F(elementID)},
onComplete: function(transport) {
var res = transport.responseText;
if (res == "false") {
bValid = false;
}
if (res == "true") {
bValid = true;
}
},
onFailure: function() {
window.location = "../error.html";
}
});
return bValid;
}
This is how I call the function:
Code:
function validateEmail() {
if ($F('email_test') != "") {
if (valEmail('email_test') == true) {
$('error').innerHTML = "valid";
} else {
$('error').innerHTML = "invalid";
}
} else {
$('error').innerHTML = "empty";
}
}