I am trying to do java script validations as well as php validations to check the existence of already present email in db.
First I am checking java script validation for particular email using regular expression.I getting that return value as true on geting the correct email format.
Then I need to do backend validations.(i.e) I need the check whether that particular email is already present in database.
I am using ajax in email field to check the existence of email.
I am getting that result too. But I need to get the return value in ajax. If email already exits i need to return value as false. SO that I can avoid form submit.
if (request.readyState == 4 && request.status == 200){
var data1 = request.responseText;
}
if(data1 == "exsist"){
document.getElementById(emailid).style.border = "2px solid blue";
return false;
function mail_exsist(elem1) {
var email = document.getElementById(elem1.id).value;
var emailid = elem1.id;
var emailval = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if (email != '') {
if (email.match(emailval)) {
var data_enter1 = email;
var dataarray = {
"value1": data_enter1
};
var data = JSON.stringify(dataarray);
var request = new XMLHttpRequest();
request.open("POST", "modal/add_email_ajax.php", true);
request.setRequestHeader("Content-Type", "application/json");
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
var data1 = request.responseText;
}
if (data1 == "exsist") {
document.getElementById(emailid).style.border = "2px solid blue";
return false;
} else {
document.getElementById(emailid).style.border = "1px solid rgba(41, 98, 255,0.4)";
return true;
}
}
request.send(data);
} else {
document.getElementById(emailid).style.border = " 1px solid red";
document.getElementById(emailid).style.outline = "0px";
return false;
}
} else {
document.getElementById(emailid).style.border = " 1px solid red";
document.getElementById(emailid).style.outline = "0px";
return false;
}
}
How to get the return value in ajax.