0

我是 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/(表单位于页面底部)

谢谢

4

1 回答 1

0

您在表单之后在 javascript 中声明 validateForm。尝试将其移至表格之前。或者更好的是,在页面的头部。

了解如何使用 javascript 调试器。您会立即发现这一点(调用 validateForm 时会引发错误)。 http://www.youtube.com/watch?v=54IgWgoFToc

于 2013-01-20T05:11:17.927 回答