繁体   English   中英

如果仅执行语句

[英]If Statement Only Executing Else Part

我有一个简单的程序可以验证注册表单。 基本上,当我的一个“ 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.");
    }
}

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

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

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

  }

工作演示

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

检查此DEMO它将满足所有条件。

注意:检查您的条件是否为密码长度,如果您想要所需的输出,则将类似于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.");
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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