gpt4 book ai didi

javascript - If 语句仅执行 else 部分

转载 作者:行者123 更新时间:2023-12-03 10:43:24 24 4
gpt4 key购买 nike

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

}

工作 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)

关于javascript - If 语句仅执行 else 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28670350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com