【问题标题】:The Verification between email addresses function will not work电子邮件地址之间的验证功能将不起作用
【发布时间】: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


【解决方案1】:

您定义了一个名为 x 的变量来保存电子邮件输入值,然后与未定义的 email 和 email2 变量进行比较。

function validateForm() {

    var email = document.forms["contactform"]["email"].value;
    if (email == "") {
        alert("E-mail is empty");
        return false;
    }

    var verificationEmail = document.forms["contactform"]["email2"].value;
    if (verificationEmail == "") {
        alert("Verification E-mail is empty");
        return false;

    } <-- !!!!!!!!!!!!! THIS WAS MISSING  !!!!!!!!!!!!

    if(email != verificationEmail){
        // 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";
    }
}

【讨论】:

  • 其实这不是一个答案,你最好把它写在第一个帖子下面作为评论。
  • 请添加更多详细信息以扩展您的答案,例如工作代码或文档引用。
【解决方案2】:

您缺少} 来终止第二个 IF,因此第三个 IF 在第二个内部,因此永远不会运行,因为它是在返回之后 :)

<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;

    } <-- !!!!!!!!!!!!! THIS WAS MISSING  !!!!!!!!!!!!

    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";
    }
#} <-- THIS WAS IN THE WRONG PLACE REMOVE IT
}
</script>

【讨论】:

  • 我明白了,但是当我将位置交换到} 时,它仍然不起作用,函数没有执行并且不显示alert("Email address does not match");
  • 不起作用不是一个很有帮助的描述。请具体
  • 函数未执行且未显示alert("Email address does not match");
  • PS 你给我们看的代码中没有alert ("verify e-mail");
  • 没有id为emailMismatch的元素可能是js崩溃了!你在看浏览器调试器吗? F12
猜你喜欢
  • 2011-04-22
  • 1970-01-01
  • 2014-06-12
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 2010-12-21
  • 2018-03-13
相关资源
最近更新 更多