【发布时间】:2021-10-27 08:19:04
【问题描述】:
我希望用户在“email2”字段中验证他的电子邮件地址,如果他输入了错误的电子邮件地址,该函数会返回错误消息并从“电子邮件”字段中更正他的电子邮件地址。此函数不返回错误但也不起作用,即当电子邮件地址不同时不报告错误消息。我正在寻求帮助。
<html>
<script>
function validateForm() {
var x = document.forms["contactform"]["email"].value;
if (x == "") {
alert("E-mail is empty");
return false;
}
var x = document.forms["contactform"]["email2"].value;
if (x == "") {
alert("Verification E-mail is empty");
return false;
if(email != email2){
// Display the error message
document.getElementById("emailMismatch").style.display="inline";
alert("Email address does not match");
return false;
}else{
document.getElementById("emailMismatch").style.display="none";
}
}
}
</script>
<body>
<form name="contactform" method="post">
<input type="text" name="email" maxlength="80" size="30" placeholder="E-mail contact person" title="The e-mail must be in the format, example: primer@email.com ">
<input type="text" name="email2" maxlength="80" size="30" placeholder="Verify E-mail address" title="The e-mail must be in the format, example: primer@email.com ">
<input type="submit" value="Send" style="border:1px solid #000000">
</form>
</body>
</html>
【问题讨论】:
-
您缺少一个
}来终止第二个 IF,因此第三个 IF 在第二个中,因此永远不会运行,因为它在return之后 :) -
什么意思,大家
}有空,再查一下。 -
你刚刚添加了缺少的
} -
@Gordon 但是在错误的地方:)
-
请注意,每个人都可以看到问题的历史记录(stackoverflow.com/posts/68952615/revisions)所以请不要试图假装
}一直在那里,它不会起作用.如果您打错了,请承认并继续前进
标签: javascript forms function validation