samedi 20 juin 2020

When the value of status is true then even the if statement is not alerting done

As you can see at last I set the status to be true when sign up is done successfully but the following if statement is now working and I don't know why. I have searched it everywhere but can't find a solution that's why I have to post this question. Please somebody help me. Thanks in advance !!

var status = false;

function isEmail(email) {
  var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return regex.test(email);
}

$("#btn").click(function () {
  var error = "";
  var missing = "";
  var regex1 = /Email/;
  var regex2 = /Number/;

  if ($("#email").val() == "") {
    missing += "<br> Email";
  }

  if ($("#number").val() == "") {
    missing += "<br> Phone Number";
  }

  if ($("#password").val() == "") {
    missing += "<br> Password";
  }

  if ($("#passwordConfirm").val() == "") {
    missing += "<br> Confirm Password";
  }

  if (missing != "") {
    error += "Following field(s) are missing:" + missing;
  }

  if (isEmail($("#email").val()) == false && missing.match(regex1) == null) {
    error += "<p> Email address is not valid </p>";
  }

  if (
    $.isNumeric($("#number").val()) == false &&
    missing.match(regex2) == null
  ) {
    error += "<p> Phone Number is not valid </p>";
  }

  if ($("#password").val() != $("#passwordConfirm").val()) {
    error += "<p> Passwords are not matching </p>";
  }

  if (error != "") {
    $("#errorMessage").html(error);
  } else {
    $("#successMessage").show();
    $("#errorMessage").hide();
    status = true;
  }

  if (status === true) {
    alert("done");
  }
});

Aucun commentaire:

Enregistrer un commentaire