我的表单验证功能有什么问题?

时间:2016-10-23 23:50:21

标签: javascript jquery html forms

我有一个简单的表格,要求姓名,电子邮件,电话号码,年龄和收入。当我点击提交时,validateForm函数应该检查错误,如果有任何错误,文本框将显示为红色,并且表单下将显示一条红色错误消息。这个功能似乎根本不起作用,我不知道为什么。 这是我的表格和脚本。我尝试设置提交按钮以使用onclick事件来调用该函数,但这样做也没有。

<script>
function validateForm() {



    if (document.forms.formValidation.Name.value == "") {

        document.getElementById("error").style.display = "inline";
        document.forms.formValidation.getElementById("name").style.backgroundColor = "red";

        return false;
    }
    if (document.forms.formValidation.email.value == "") {
        document.getElementById("error").style.display = "inline";
        document.forms.formValidation.email.style.backgroundColor = "red";
    }
    var phoneNo = /^\d{10}$/;
    if (document.forms.formValidation.number.value.match(phoneNo)) {
        return true;
    }
    else {
        document.getElementById("error").style.display = "inline";
        document.forms.formValidation.number.style.backgroundColor = "red";
        return false;
    }

    var emailVal = document.formValidation.email.value;
    ampersandPos = emailVal.indexOf("@");
    dotPos = emailVal.lastIndexOf(".");

    if (amersandPos < 1 || (dotPos - amersandPos < 2)) {
        alert("Please enter a valid email address.");
        document.getElementById("error").style.display = "inline";
        return false;
    }
    return true;

    var len = age.length;
    for (var i = 0; i < len; i++) {
        if (document.forms.formValidation.age[i].checked)
        {
            return true;
        }
        else {
            document.getElementById("error").style.display = "inline";
            return false;
        }
    }




}

</script>

<form name="formValidation" onsubmit="return validateForm()">
                    <table cellspacing="2" cellpadding="2" border="1">
                        <tr>
                            <td align="right">First and Last Name: </td>
                            <td align="left"><input type="text" id="name" name="Name" ></td>
                        </tr>
                        <tr>
                            <td align="right">Email: </td>
                            <td align="left"><input type="text" id="email" name="email" ></td>
                        </tr>
                        <tr>
                            <td align="right">Phone Number: </td>
                            <td align="left"><input type="text" name="number" /></td>
                        </tr>
                        <tr>
                            <td align="right">Age:</td>
                            <td class="rad"><input type="radio" name="age" value="minor">Under 18</td>
                            <td class="rad"><input type="radio" name="age" value="adult">18-64</td>
                            <td class="rad"><input type="radio" name="age" value="senior">65+</td>
                        </tr>
                        <tr>
                            <td align="right">Income:</td>
                            <td>
                                <select>
                                    <option value="underFifty">Under $50,000</option>
                                    <option value="fiftyToHundred">$50,000 - $100,000</option>
                                    <option value="overHundred">Over $100,000</option>
                                </select>
                            </td>
                        </tr>

                    </table>
                    <input type="submit" value="Submit">                
                    </form>
                    <div id="error">
                        <p>Required fields are missing!</p>
                    </div>

2 个答案:

答案 0 :(得分:1)

您在此行中缺少右括号:

if (amersandPos < 1 || (dotPos - amersandPos < 2)

此外,此行无效:

document.formValidation.email.style.background-color = "red";

因为background-color不是有效的标识符名称。

这里有一个小提琴,并且评论中的一个问题已修复:https://jsfiddle.net/0zyv3g98/

答案 1 :(得分:0)

你在js中犯了错误。你不能使用document.formValidation.name或类似的东西。正确的语法是`document.forms.formValidation。更正所有代码,它将起作用。

      if (document.forms.formValidation.Name.value == "") {

        document.forms.formValidation.getElementById("error").style.display = "inline";
        document.forms.formValidation.getElementById("name").style.backgroundColor = "red";

        //return false;
    }
    if (document.forms.formValidation.email.value == "") {
        document.forms.formValidation.getElementById("error").style.display = "inline";
        document.forms.formValidation.email.style.backgroundColor = "red";
    }

    var emailVal = document.forms.formValidation.email.value;
    ampersandPos = emailVal.indexOf("@");
    dotPos = emailVal.lastIndexOf(".");

    if (amersandPos < 1 || (dotPos - amersandPos < 2)
    {
        alert("Please enter a valid email address.");
        document.forms.formValidation.getElementById("error").style.display = "inline";
        return false;
    }
    return true;