Hi guys please help me to solve my problem. Actually i am trying to validate my email field on form using javascript and post an ajax request to check if that email exists or not but my javascript function does not wait until the ajax response and every time it returns false. I want to be return true when email does not exist...
$(document).ready(function(){
//global vars
var form = $("#signupForm");
var email = $("#email");
var email_error = $("#invalid");
//On blur
// userEmail.blur(validateEmail);
//On Submitting
form.submit(function(){
if(validateEmail()){
alert("submit true");
return true;
}else{
alert("submit false");
return false;
}
});
function validateEmail(){
//if it's NOT valid
alert("In email");
var a = $("#email").val();
var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
if(email.val() == ""){
email_error.html("Please enter your email ");
return false;
}
//if it's valid email
else if(filter.test(a)==false){
email_error.html("Please enter Valid email address");
return false;
}
else if(filter.test(a)==true){alert("elseif");
var baseUrl = '<?= base_url() ?>';
$.ajax({
type: "POST",
url: baseUrl+"index.php/index/validateUserEmail",
data: "useremail="+$("#email").val(),
success: function(msg){
alert(msg);
if(msg == "1")
{
$("#tick_cross").fadeIn("slow").html('<img src="'+baseUrl+'images/cross.png" />');
$("#emailValidate").val("1");
email_error.html("This email-Id already exists!");
return false;
}
else
{
$("#tick_cross").fadeIn("slow").html('<img src="'+baseUrl+'images/tick.png" />');
$("#emailValidate").val("0");
email_error.html(" ");
alert("alok");
return true;
}
}
});
}
else{
email_error.html(" ");
return true;
}
}
});