簡體   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