为什么我的表单没有验证提交?

时间:2013-01-20 05:06:23

标签: javascript forms function validation onsubmit

我是javascript的新手,我正试图让它在用户没有填写表单上的所有字段时,他们会收到警告错误。

以下是我的表单的第一行,告诉它在提交时验证我的功能......

<form accept-charset="UTF-8" action="xxxx" class="infusion-form" method="POST" name="myform" onsubmit="return validateForm();">

这是我的javascript函数......

<script type="text/javascript">// <![CDATA[
function validateForm() {
    var a = document.forms["myform"]["inf_field_FirstName"].value;
    var b = document.forms["myform"]["inf_field_Email"].value;
    var c = document.forms["myform"]["inf_field_Phone1"].value;
    var d = document.forms["myform"]["inf_field_StreetAddress1"].value;
    var e = document.forms["myform"]["inf_field_City"].value;
    var f = document.forms["myform"]["inf_field_State"].value;
    var g = document.forms["myform"]["inf_field_PostalCode"].value;
    var h = document.forms["myform"]["inf_other_Username"].value;
    var i = document.forms["myform"]["inf_other_Password"].value;
    var j = document.forms["myform"]["inf_other_RetypePassword"].value;

    if (a == null || a == "" || a == "First Name Here") {
        alert("Please enter your First Name!");
        return false;
    }
    if (c == null || c == '' || c == "Enter Your Phone Here" || c.length < 9) {
        alert("Please insert your phone number!");
        return false;
    }
    if (d == null || d == '' || d == "Street Address") {
        alert("Please insert your street address ");
        return false;
    }
if (e == null || e == '' ||e == "City") {
        alert("Please insert your city");
        return false;
    }

    if (f == null || f == '' || f == "State") {
        alert("Please insert your state ");
        return false;
    }

if (g == null || g == '' ||g == "Postal Code") {
        alert("Please insert your postal code");
        return false;
    }
    if (h == null || h == '' || h == "Username") {
        alert("Please insert your username ");
        return false;
    }
if (i == null || i == '' ||i == "password") {
        alert("Please insert your password");
        return false;
    }
    if (j == null || j == '' || j == "password") {
        alert("Please re - type your password ! ");
        return false;
    }




    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if (document.myform.inf_field_Email.value.search(emailRegEx) == -1) {
        alert("Please enter a valid email address.");
        return false;
    }
}
// ]]>
</script>

我不确定我做错了什么?非常感谢你的帮助。如果你想检查元素(表单位于页面底部)

,页面也在http://business.uglyopportunities.com/affiliate-signup/

感谢

1 个答案:

答案 0 :(得分:0)

您在表单后面的javascript中声明了validateForm。尝试将其移动到表单之前。甚至更好,在页面的头部。

了解如何使用javascript调试器。您可能会立即捕获此信息(validateForm在调用时会抛出错误)。 http://www.youtube.com/watch?v=54IgWgoFToc