2013-02-18 55 views
0

我想驗證一個窗體使用Javascript,但密碼長度檢查不起作用,但沒有明顯的錯誤代碼從我可以看到。Javascript表單驗證沒有檢查給定的變量

形式:

<form id="reg_form" onsubmit="return false;"> 
     <input type="text" maxlength="255" placeholder="Email..." name="email" /><br /> 
     <input type="password" placeholder="Password..." name="password" /><br /> 
     <input type="text" maxlength="30" placeholder="Username..." name="username" /><br /> 
     <input type="text" maxlength="255" placeholder="First Name..." name="fname" /><br /> 
     <input type="text" maxlength="255" placeholder="Last Name..." name="lname" /><br /> 

     <!-- Start Div With reCaptcha In --> 

     <!-- End Div With reCaptcha In --> 

     <input type="submit" value="Sign-up" onclick="register(this.form, this.form.email, this.form.password, this.form.username, this.form.fname, this.form.lname);" /> 
    </form> 

JavaScript函數:

<script> 
    function register(form, email, password, username, fname, lname) { 
    document.getElementById("reg_error").style.display = "block"; 
    if (email.value.length < 1) { 
     document.getElementById("reg_error").innerHTML = "Please Enter An Email Address!"; 
     return; 
    } 
    if (password.value.length < 6) { 
     document.getElementById("reg_error").innerHtml = "Passwords Must Be Longer Than 6 Characters!"; 
     return; 
    } 
    if (username.value.length < 5 || username.value.length > 30) { 
     document.getElementById("reg_error").innerHTML = "Username Must Be Between 5 And 30 Characters Long!"; 
     return; 
    } 
    if (fname.value.length < 1) { 
     document.getElementById("reg_error").innerHTML = "Please enter a first name!"; 
     return; 
    } 
    if (lname.value.length < 1) { 
     document.getElementById("reg_error").innerHTML = "Please enter a last name!"; 
     return; 
    } 
    var info = new FormData(); 
    info.append("email", email.value); 
    info.append("p", hex_sha512(password.value)); 
    password.value = ""; 
    info.append("username", username.value); 
    info.append("fname", fname.value); 
    info.append("lname", lname.value); 
    if (window.XMLHttpRequest) { 
     register_ = new XMLHttpRequest(); 
    } else { 
     register_ = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    register_.onreadystatechange = function() { 
     if (register_.readyState == 4 && register_.status == 200) { 
     document.getElementById("reg_error").innerHTML = register_.responseText; 
     } 
    } 
    register_.open("POST", "scripts/signup.php", true); 
    register_.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    register_.send(info); 
    } 
</script> 

所有的if語句從查詢密碼值長度的一個正常工作分開。任何幫助,將不勝感激。正如我通過console.log()測試它們一樣,值正在正確傳遞。

我正在使用HTML5 Boilerplate。我不想用jQuery來做到這一點。

回答

0

你在這部分拼錯innerHTML

if (password.value.length < 1) { 
    document.getElementById("reg_error").innerHtml = ... 

將其更改爲:

document.getElementById("reg_error").innerHTML = ... 
+0

由於這個工作。討厭缺少像這樣的小東西:)我會盡我所能接受你的答案。 – 2013-02-18 01:37:26