如果声明仅执行其他部分

时间:2015-02-23 09:20:19

标签: javascript regex if-statement

我有一个验证注册表单的简单程序。当我的一个“if”语句只能执行“else”部分时,基本上我遇到了问题。如果我更改表单以便执行“if”,则不会发生任何事情。以下是我的代码:

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.");
    }
}

2 个答案:

答案 0 :(得分:1)

您是否检查过它是否正常工作?

无论如何它对我有用,我认为您的输入应无效,您可以使用以下代码进行检查

 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.");
            }

  }

正在使用 DEMO

<强>更新

    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.");
    }

选中 DEMO 它将满足所有条件。

注意:检查密码长度的if条件,如果你想要所需的输出,那么它就像if (originalpassword.length > 9)

答案 1 :(得分:0)

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.");
        }