I am using this code for email validation:
function validateEmail(){
//testing regular expression
var a = $("#email").val();
$.ajax({
type: "GET",
url: "is_email.php",
data: "email="+a,
success: function(response){
//if it's valid email
if(is_email(a)){
email.removeClass("error");
emailInfo.text("");
emailInfo.removeClass("error");
return true;
//if it's NOT valid
}else{
email.addClass("error");
emailInfo.text("Please type a valid E-mail");
emailInfo.addClass("error");
return false;
}
}
});
}
But I get this error in firebug: is_email is not defined.
How can I correct this?
Thank you
Looks like you’re trying to use a PHP function in JavaScript. That’s not possible, but if the is_email.php script does the validation and prints something depending on the result, you can use that.
Let’s assume that the is_email.php prints “ok” when the email is valid (change to whatever the actual output is):
But, I suspect that is_email.php only contains the
is_email()function. In this case you must make another PHP script that actually uses that function and use it in the Ajax call instead:then change the
url: "is_email.php"line in the Ajax call tourl: "validate_email.php"(or whatever the new script is called).Also note that returning true or false from the callback function in the Ajax is meaningless: there’s nothing to receive that information.
validateEmail()has already long since returned when the Ajax call has completed. You have to do everything you want to do inside the callback function.