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

时间:2013-01-19 21:34:22

标签: javascript forms validation submit form-submit

我在http://business.uglyopportunities.com/affiliate-signup/的页面上有一个注册表单(向下滚动查看表单)

请记住,我对JS不是很好,所以这可能是一个简单的错误。

无论如何,这是表格的第一行,告诉它验证:

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

这是我的validateForm javascript:

<script type="text/javascript">
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>

那是行不通的。但是,当我使用此代码时,它工作正常:

<script type="text/javascript">
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; 
    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;
    }

    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>

更新后的代码 *

<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>

2 个答案:

答案 0 :(得分:1)

这里有一个空格:

var d = document.forms["myform"][" inf_field_StreetAddress1"].value;
//                                ^-----------------------

e,f,g,h,i,j

存在同样的问题

另外,您使用了错误的引号:

"Street Address”
//             ^--------------

你做了好几次。

答案 1 :(得分:0)

正如gdoron所说,这可能是一个拼写问题。您无法使用空格http://www.w3.org/TR/html401/types.html#type-name

开始元素的名称