简体   繁体   中英

If Statement Only Executing Else Part

I have a simple program that validates a sign up form. Basically I reach a problem when one of my "if" statements can only execute the "else" part. If I change the form so that "if" is executed, nothing happens. Below is my code:

function verifyprocedure() {
    var originalpassword = document.getElementById("password").value;
    var checkpassword = document.getElementById("verifypassword").value;
    var emailcheck = document.getElementById("email").value;
    var male = document.getElementById("gendermale");
    var female = document.getElementById("genderfemale");
    var form = document.getElementById("signup");
    var emailexist = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (originalpassword == checkpassword) {
        if (originalpassword.length < 9) {
            if (emailexist.test(emailcheck)) //this is the statement that does not work
            {
                alert("Hello World!"); //this is whats supposed to be executed
            } else { //this is successfully executed
                $("#email").notify("Please enter a valid email address.");
            }
        } else {
            $("#password").notify("Passwords must have at least 9 characters.");
        }

    } else {
        $("#verifypassword").notify("Passwords do not match.");
    }
}

Did you check whether it is working or not ?

anyway it is working for me,i think your input should be invalid you can use the following code for checking

 var originalpassword ="abcdefgh";
    var checkpassword = "abcdefgh";
    var emailcheck = "arun@gmail.com";
    var male ="male";


    var emailexist = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (originalpassword == checkpassword) {
      alert("hai");
}

  if (originalpassword.length < 9) {
       if (emailexist.test(emailcheck)) 
            {
                alert("Hello World!"); 
            } else { 
              alert("Please enter a valid email address.");
            }

  }

Working DEMO

UPDATE

    var originalpassword ="abcdefgh";
    var checkpassword = "abcdefgh";
    var emailcheck = "arun@gmail.com";
    var male ="male";

    var emailexist = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (originalpassword == checkpassword) 
    {
      alert("hai");
              if (originalpassword.length > 9)
          {
               if (emailexist.test(emailcheck)) 
                {
                alert("Hello World!"); 
                } 
              else
              { 
              alert("Please enter a valid email address.");
                }

          }
         else
             {
           alert("Passwords must have at least 9 characters.");
            }

    }
else {
       alert("Passwords do not match.");
    }

Check this DEMO It will satisfy all conditions.

Note : Check your if condition for password length, if you want the desired output then it will be like if (originalpassword.length > 9)

if (originalpassword.length > 9) {
            if (emailexist.test(emailcheck)) //this is the statement that does not work
            {
                alert("Hello World!"); //this is whats supposed to be executed
            } else { //this is successfully executed
                $("#email").notify("Please enter a valid email address.");
            }
        } else {
            $("#password").notify("Passwords must have at least 9 characters.");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM