JS验证以正常速度失败,但在单步执行代码时有效

时间:2012-06-26 13:25:45

标签: javascript jquery forms validation

当我单步执行时,js验证工作正常,但在“正常速度”下失败。 特别说明:如果捕获了重复的电子邮件但其他字段已正确填写,则可以提交表单(但是在单步执行代码时不会出现错误)。谁看过这个吗?我知道我可以用不同的方式对它进行编码,但我不能简单地摆脱这个简单的问题,即使没有先理解为什么这不起作用,它甚至会成为一个瓶子跪下。

我的方法是验证onblur和onsubmit。我只是为了方便而使用jquery选择器,然后再次使用ajax调用,否则我正在使用js。我在字段中循环,但只在文本和密码字段上操作。 检查空白 检查名字中没有数字 检查格式正确的电子邮件地址 然后在电子邮件字段中检查唯一的电子邮件 js和下面的注释代码如下:

//registration validation
$('.validate').blur(function() {
var theForm = document.registerNewUserForm;         
//removes error messages from html before the run
clearAllErrors(theForm);
var msg = "";
var mdiv;
theForm.submit.disabled=true;
document.getElementById("submitButton").disabled = true;

for (var i = 0; i < theForm.elements.length; i++) {

    //mdiv is set to form element being evaluated at the time
    mdiv = document.getElementById(theForm.elements[i].name + "Message");

    msg = validateField(theForm.elements[i]);
    if(msg != "") {
        mdiv.innerHTML = msg;
        break;
    }
}
if(msg == "") {
    theForm.submit.disabled=false;
    document.getElementById("submitButton").disabled = false;
}
});


$('#registerNewUserForm').submit(function() {
var theForm = document.registerNewUserForm;
clearAllErrors(theForm);
var msg = "";
var mdiv;
for (var i = 0; i < theForm.elements.length; i++) {

    //mdiv is set to form element being evaluated at the time
    mdiv = document.getElementById(theForm.elements[i].name + "Message");

    msg = validateField(theForm.elements[i]);
    if(msg != "") {
        break;
    }
}
if (msg != ""){
    mdiv.innerHTML = msg;
    return false;       
}  else {
    theForm.submit();
}   
});

function validateField(theField) {
msg = "";   
//all fields are required
if (theField.type == "text" || theField.type == "password") {
    if (theField.value == "") {
        msg = "The " + theField.name + " field is required.";
    }
}

//name fields are non-numeric
if (theField.name == "fullName"){
    if (hasNumber(theField.value) == true){
        msg= "The Name field is non-numeric.";
    }
}

//email must be correctly formatted
if (theField.name == "email") {
    msg = emailCheck (theField.value);
    if (msg == "") {
        //email address must also be unique
        chkEmail();
        msg = document.getElementById('emailMessage').innerHTML;
    }
}   
return msg;
}

function chkEmail() {
emailAddr = document.getElementById("email").value;
$.ajax({
    url: '/chkEmail',
    type: 'POST',
    data: 'emailAddr=' + encodeURIComponent(emailAddr),
    dataType: "xml",
    context: document.body,
    success: function(data) {
        document.getElementById('emailMessage').innerHTML = $(data).find("message").text();
    }
});
}


<form name="registerNewUserForm" id="registerNewUserForm" action="/register" method="post">
    <br/>
    <div>Create an Account and join the fun!</div>
    <div><input class="validate" type="text" id="fullName" required placeholder="Full Name" name="fullName" value="" size="30"></div>
    <div id="fullNameMessage" class="error"></div>
    <div><input class="validate" type="text" id="email" required placeholder="Email Address" name="email" value="" size="30"></div>
    <div id="emailMessage" class="error"></div>
    <div><input class="validate" type="password" id="passWord" required placeholder="Password" name="passWord" value="" size="30"></div>
    <div id="passWordMessage" class="error"></div>
    <div style="position:relative;left:173px;"><input id="submitButton" type="submit" value="Signup for PastelPlanet"></div>
    <input type="hidden" name="formName" value="registerNewUserForm">
    <input type="hidden" name="urlDestination" value="">
</form>

1 个答案:

答案 0 :(得分:2)

您的“chkEmail”功能涉及对服务器的调用,它是异步。当你以“全速”运行时,当函数返回时,对服务器的调用将不会完成。