当最后一个条目正确时,onsubmit不会被触发

时间:2013-07-12 20:18:28

标签: javascript html javascript-events

当电子邮件输入中没有任何内容时,一切正常。但是一旦我输入“有效”的电子邮件地址,它甚至不会触发该功能,因为警报永远不会被触发。任何帮助将不胜感激。

Javascript(js / registration.js):

function validateForm() {
var value1 = document.forms["regForm"]["username"].value;
var value2 = document.forms["regForm"]["pass1"].value;
var value3 = document.forms["regForm"]["pass2"].value;
var value4 = document.forms["regForm"]["email"].value;
var atpos = value4.indexOf("@");
var dotpos = value4.lastIndexOf(".");
var check = true;

if(value1 == null || value1 == "") {
    $("#userCheck").html("<img src='images/ico_fail.png'/> Please enter a user name! (25 character limit)");
    check = false;
} else $("#userCheck").html("<img src='images/ico_pass.png'/>");

if(value2 == null || value2 == "") {
    $("#pass1Check").html("<img src='images/ico_fail.png'/> Please enter a password! (25 character limit)");
    check = false;
} else {
    if(value2.length < 8) {
        $("#pass1Check").html("<img src='images/ico_fail.png'/> Password must be 8 to 25 characters");
        check = false;
    } else $("#pass1Check").html("<img src='images/ico_pass.png'/>");
}

if(value3 == null || value3 == "") {
    $("#pass2Check").html("<img src='images/ico_fail.png'/> Please re-enter your password! (25 character limit)");
    check = false;
} else {
    if(value3 != value2) {
        $("#pass2Check").html("<img src='images/ico_fail.png'/> Password does not match!");
        check = false;
    } else $("#pass2Check").html("<img src='images/ico_pass.png'/>");
}

if(value4 == null || value4 == "") {
    $("#emailCheck").html("<img src='images/ico_fail.png'/> Please enter a valid email!");
    check = false;
} else {
    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
        $("#emailCheck").html("<img src='images/ico_fail.png'/> Please enter a valid email!");
        check = false;
    } else $("#emailCheck").html("<img src='images/ico_pass.png'/>");
}

alert(check);
return check;
}

HTML:

<form name="regForm" method="post" action="" onsubmit="return validateForm();">
    <input type="text" id="uName" name="username" size="25" maxlength="25" value=""/>
    <span id="userCheck"> (25 character limit)</span></br>
    <input type="password" id="password1" name="pass1" size="25" maxlength="25" value=""/>
    <span id="pass1Check"> (25 character limit)</span></br>
    <input type="password" id="password2" name="pass2" size="25" maxlength="25" value=""/>
    <span id="pass2Check"></span></br>
    <input type="text" name="email" size="25" value="" />
    <span id="emailCheck"></span></br>
    <input type="submit" name="submit" value="Register" />
    <input type="hidden" name="perm" value="3" /> <!-- regular user -->
    <input type="hidden" name="redirect" value="index.php" />
</form>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="js/registration.js"></script>

2 个答案:

答案 0 :(得分:1)

问题是:ReferenceError: x is not defined

在这一行:

if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {

答案 1 :(得分:1)

if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {

你这里没有定义x。我认为它应该是value4.length