0

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.

Phil
  • 157,677
  • 23
  • 242
  • 245
Jasmine Joseph
  • 287
  • 2
  • 21
  • 1
    Consider using indentation when writing code - it'll make debugging it much easier, not only for others, but for you as well, when you can see where the blocks are at a glance. – CertainPerformance Nov 30 '18 at 04:38
  • Looks like your `add_email_ajax.php` script needs to respond with either `exsist` or literally anything else. This will determine the border colour of your `emailid` element. You cannot use the `return true` or `return false` to prevent or cause a form submission though since at that stage, it's too late – Phil Nov 30 '18 at 04:45

0 Answers0