JavaScript表单的onsubmit处理(JavaScript form onSubmit h

2019-10-17 16:33发布

我是新来的JavaScript,并已在这里寻找线程,但无法弄清楚,为什么我的形式不是通过审定运行正常。 我曾尝试添加警示找到问题,现在看来似乎是某处大约第一个IF语句。 有任何想法吗? 谢谢!

<script type="text/javascript">

function dataCheck()
{
        var firstName = document.getElementById("firstName").value;
        var firstName = document.myForm.firstName.value;
        var lastName = document.getElementById("lastName").value;
        var facilitator = document.getElementById("facilitator").value;
        //validate firstName field for minimum of 2 characters
        if (firstName.length < 2){
            alert("Please enter a first name with a minimum of 2 characters");
            return false;
        }
        else {retvalue = true}
        //validate lastName field for min length and numeric or alpha characters only
        if (lastName.length < 2 || lastName.){
            alert("Please enter a last name with a minimum of 2 characters");
            return false;
        }
        if( /[^a-zA-Z0-9]/.test(lastName) ) {
            alert("Last name must be alphanumeric.");
            return false;
        }
}

</script>

<form action="http://bucs601.com/submit.php" name="myForm" onsubmit="return dataCheck()" method="post">
              First name: <input type="text" id="firstName" name="firstName"><br>
              Last name: <input type="text" id="lastName" name="lastName"><br>
              Email: <input type="text" id="email" name="email"><br>
              Facilitator: <input type="text" id="facilitator" name="facilitator">
              <input type="submit" id="submit" value="Submit">
</form>

Answer 1:

在一个明显的错误,我可以看到的是不完整的线路:

if (lastName.length < 2 || lastName.){

什么是应该是第二个条件? 如果删除了“|| lastName的。” 位的功能似乎工作正常。



Answer 2:

应该是这样的:

替换if (lastName.length < 2 || lastName.){

if (lastName.length < 2 || lastName){

删除.



文章来源: JavaScript form onSubmit handling