0

我有 JavaScript 代码,可以在第一次提交请求时验证表单,但是如果用户再次点击提交,即使他们没有纠正表单错误,表单也不会处理验证功能,只会提交。

我希望每次用户单击提交按钮时都执行验证功能。

感谢您的帮助,代码如下。马丁

<form action="site.url" method="post" name="signup" onsubmit="return validateForm()"> 
    <label for="firstname"><span id="inactiveErrorFname">Please enter your first name<br></span>
        First name <strong title="Required" class="required">*</strong></label><br>
    <input type="text" name="firstName">
    <label for="surname"><br><span id="inactiveErrorSname">Please enter your Surname<br></span>
        Surname</label><input type="text" name="lastName">  
    <label for="email">
        <span id="inactiveErrorEmail"><br>Please enter your Email address<br></span>
        Email address <strong title="Required" class="required">*</strong></label>
    <input type="text" name="emailAddress">
    <input class="button" type="submit" value="Sign up" />    
</form>

<script>

    function validateForm()
    {
        var x=document.forms["signup"]["firstName"].value;
        var y=document.forms["signup"]["lastName"].value;
        var z=document.forms["signup"]["emailAddress"].value;
        var atpos=z.indexOf("@");
        var fname;
        var sname;
        var email;

        /* Validate first name */
        if (x==null || x=="")
        {
            document.getElementById("inactiveErrorFname").id = "activeErrorFname";
            fname = "true";
        }
        /* Validate Surname */
        if (y==null || y=="")
        {
            document.getElementById("inactiveErrorSname").id = "activeErrorSname";
            sname = "true";
        }
        /* Validate email */
        if (atpos<1)
        {
            document.getElementById("inactiveErrorEmail").id = "activeErrorEmail";
            email = "true";
        }
        if (fname=="true" || sname=="true" || email =="true")
        {
            return false;
        }
    }

</script>
4

1 回答 1

0

您面临的问题是由于您正在更改某些元素的 id 而造成的。第一次运行代码时,这些语句会成功运行:

document.getElementById("inactiveErrorFname").id = "activeErrorFname";
document.getElementById("inactiveErrorSname").id = "activeErrorSname";
document.getElementById("inactiveErrorEmail").id = "activeErroremail";

然而,第二次这些元素的 id 发生了变化,导致这些语句调用了不存在的元素。这会导致 javascript 致命错误,结果是 no return false。因此,您的表单第二次将照常提交。这个问题的解决方案不是改变id,而是改变错误标签的类别。你可以在这个 fiddle中找到一个例子。

HTML:

<form action="#" method="post" name="signup" onsubmit="return validateForm()"> 
    <label for="firstname">
      <span class="inactive" id="errorFname">Please enter your first name<br></span>
      First name <strong title="Required" class="required">*</strong>
    </label><br>
    <input type="text" name="firstName"><br><br>

    <label for="surname">
      <span class="inactive" id="errorSname">Please enter your Surname<br></span>
      Surname
    </label><br>
    <input type="text" name="lastName"><br><br>

    <label for="email">
        <span class="inactive" id="errorEmail"><br>Please enter your Email address<br></span>
        Email address <strong title="Required" class="required">*</strong>
    </label><br>
    <input type="text" name="emailAddress"><br><br>

    <input class="button" type="submit" value="Sign up" />    
</form>

Javascript:

function validateForm()
{
    var x=document.forms["signup"]["firstName"].value;
    var y=document.forms["signup"]["lastName"].value;
    var z=document.forms["signup"]["emailAddress"].value;
    var atpos=z.indexOf("@");
    var fname;
    var sname;
    var email;

    /* Validate first name */
    if (x==null || x=="")
    {
        document.getElementById("errorFname").className = "active";
        fname = "true";
    }
    /* Validate Surname */
    if (y==null || y=="")
    {
        document.getElementById("errorSname").className = "active";
        sname = "true";
    }
    /* Validate email */
    if (atpos<1)
    {
        document.getElementById("errorEmail").className = "active";
        email = "true";
    }
    if (fname=="true" || sname=="true" || email =="true")
    {
        return false;
    }
    return false;
}
于 2016-05-11T20:52:47.877 回答