gpt4 book ai didi

javascript - 比较两个表单电子邮件字段的值

转载 作者:行者123 更新时间:2023-11-28 03:27:18 25 4
gpt4 key购买 nike

我创建了一个需要电子邮件验证的表单。因此,用户必须输入他们的电子邮件地址两次,如果不匹配,他们将无法提交。我通过简单地比较电子邮件字段 1 和 2 的值来完成此操作。如果它们匹配,则从提交按钮中删除“已禁用”。

当我将值设置为“插入您的电子邮件地址并再次确认您的电子邮件地址”时,一切工作正常。但是,为了用户不必删除该文本,我删除了该值并使用了“占位符” "改为 HTML 中。

现在的问题是,当你输入任何内容时,它就会返回 true。我猜它看到的空白值是一样的,但是当用户输入值时它没有注意到值的更改。

为什么这两个字段总是作为匹配项返回?

<html>
<body>
<form class="theForm">
<p> Subscribe to my mailing list</p>
<input type="text" id="name" class="fields" name="name" placeholder="Name">
<input type="text" id="email1" class="fields" name="email" placeholder="Email Address" >
<input type="text" id="email2" class="fields" placeholder="Confirm Email Address" >
<input name="submit" id="submit" class="fields" type="submit" disabled value="Email Addresses
Do Not Match">
</form>
<script>
function verify (){

console.log(`email1.value: ${email1}: Email2: ${email2}`);

if(document.getElementById("email1").value === document.getElementById("email2").value) {

document.getElementById("submit").removeAttribute("disabled");
document.getElementById("submit").style.backgroundColor = "#004580";
document.getElementById("submit").style.cursor = "pointer";

} else {
document.getElementById("submit").setAttribute("disabled", "disabled");
}
}
$(".fields").on("change paste keyup", verify);
</script>
</body>
</html>

最佳答案

试试这个

<html>
<body>
<form class="theForm">
<p> Subscribe to my mailing list</p>
<input type="text" id="name" class="fields" name="name" placeholder="Name">
<input type="text" id="email1" class="fields" name="email" placeholder="Email Address" >
<input type="text" id="email2" class="fields" placeholder="Confirm Email Address" >
<input id="submit" type="button" onclick="verify()" value="click">
</form>
<script>
function verify()
{
if(document.getElementById("email1").value === document.getElementById("email2").value) {

alert("matched")

} else {
document.getElementById("submit").setAttribute("disabled", "disabled");
alert("not matched")
}

}

</script>
</body>
</html>

关于javascript - 比较两个表单电子邮件字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58515116/

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