2016-02-10 52 views
1

深入瞭解JS,因爲我沒有弄錯它的工作。試圖編寫一些表單驗證,並在第一個輸入時保持「停止」。任何意見是極大的讚賞。我已經嘗試過所有我能想到的事情,閱讀了很多關於表單驗證的文章,但沒有任何作品。如果任何人都可以指出我的錯誤和「爲什麼」他們是錯誤,那將是非常感激。這裏是我的代碼:JS窗體驗證上的磚牆問題

 <?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 

    <script language="javascript" type="text/javascript"> 

function validateForm() { 
var validFirst = document.forms["myForm"]["fname"].value; 

if (validFirst == null || validFirst == "") { 
    document.getElementById("firstError").innerHTML = "Required Field"; 
    document.getElementById("first").style.backgroundColor = "red"; 
    return false; 
} 
else if (validFirst.length < 2) {   
    document.getElementById("firstError").innerHTML = "Response too short"; 
    document.getElementById("first").style.backgroundColor = "red"; 
    return false; 
    } 
else if (validFirst !== null) {   
    document.getElementById("firstError").innerHTML = ""; 
    document.getElementById("first").style.backgroundColor = "white"; 
    return false; 
    } 

var validLast = document.forms["myForm"]["lname"].value; 
if (validLast == null || validLast == "") { 
    document.getElementById("lastError").innerHTML = "Required Field"; 
    document.getElementById("last").style.backgroundColor = "red"; 
    return false; 
} 
else if (validLast.length < 2) {   
    document.getElementById("lastError").innerHTML = "Response too short"; 
    document.getElementById("last").style.backgroundColor = "red"; 
    return false; 
    } 
else if (validLast !== null) {   
    document.getElementById("lastError").innerHTML = ""; 
    document.getElementById("last").style.backgroundColor = "white"; 
    return false; 
    } 
    var validEmail = document.forms["myForm"]["email"].value; 
if (validEmail == null || validEmail == "") { 
    document.getElementById("emailError").innerHTML = "Required Field"; 
    document.getElementById("email").style.backgroundColor = "red"; 
    return false; 
} 
else if (validEmail.length < 2) {   
    document.getElementById("emailError").innerHTML = "Response too short"; 
    document.getElementById("email").style.backgroundColor = "red"; 
    return false; 
    } 
else if (validEmail !== null) {   
    document.getElementById("emailError").innerHTML = ""; 
    document.getElementById("email").style.backgroundColor = "white"; 
    return false; 
    } 

    var validPhone = document.forms["myForm"]["phone"].value; 
if (validPhone == null || validPhone == "") { 
    document.getElementById("phoneError").innerHTML = "Required Field"; 
    document.getElementById("phone").style.backgroundColor = "red"; 
    return false; 
} 
else if (validEmail.length < 2) {   
    document.getElementById("phoneError").innerHTML = "Response too short"; 
    document.getElementById("phone").style.backgroundColor = "red"; 
    return false; 
    } 
else if (validEmail !== null) {   
    document.getElementById("phoneError").innerHTML = ""; 
    document.getElementById("phone").style.backgroundColor = "white"; 
    return false; 
    } 
    } 

</script> 
<head> 
<title>Form Validation</title> 

<form name="myForm" action="" onsubmit="return validateForm()" method="post"> 
<table> 
     <tr> 
      <td>First Name</td> 
      <td><input type="text" id="first" name="fname" onchange="validFirst()"></td> 
      <td><span id="firstError"></span></td> 
     </tr> 

      <td>Last Name</td> 
      <td><input type="text" id="last" name="lname" onchange="validLast()"></td> 
       <td><span id="lastError"></span></td> 
     </tr> 

     <tr> 
      <td>Email</td> 
      <td><input type="text" id="email" name="email" onchange="validEmail()"></td> 
      <td><span id="emailError"></span></td> 
     </tr> 
     <tr> 
      <td>Phone</td> 
      <td><input type="text" id="phone" name="phone" onchange="validPhone()"></td> 
      <td><span id="phoneError"></span></td> 

     <tr> 
      <td><input type="submit" value="Submit"></td> 
      <td></td> 
      <td></td> 
      </tr> 
</table> 

+0

你錯過了'',一個''在你的HTML一個''。確保這不是問題的一部分。 – Blazemonger

回答

0

在每一節的最後else聲明,您傳回表單域的「非錯誤「狀態,然後返回false,導致驗證功能退出。

的解決方法是刪除各組在這最後return false;if/else if塊:

if (validFirst == null || validFirst == "") { 
    document.getElementById("firstError").innerHTML = "Required Field"; 
    document.getElementById("first").style.backgroundColor = "red"; 
    return false; 
} 
else if (validFirst.length < 2) {   
    document.getElementById("firstError").innerHTML = "Response too short"; 
    document.getElementById("first").style.backgroundColor = "red"; 
    return false; 
    } 
else if (validFirst !== null) {   
    document.getElementById("firstError").innerHTML = ""; 
    document.getElementById("first").style.backgroundColor = "white"; 
// return false; /**** this is causing your validation function to terminate ****/ 
    } 

https://jsfiddle.net/mblase75/75rcwv8k/

+0

把他們拿出來/改變最後一個「返回true」也不會使它工作。 – chrisclarkux

+0

那麼你必須爲我澄清這個問題。 [它似乎在這個小提琴中工作得很好。](https://jsfiddle.net/mblase75/75rcwv8k/) – Blazemonger

+0

第一個項目有效,但其餘的則沒有,當它們都應該表現完全相同時。你提供的小提琴做同樣的事情;只適用於名字元素。 – chrisclarkux

0

固定!感謝大家的幫助!

function validateForm() { 

var validFirst = document.forms["myForm"]["fname"].value; 

if (validFirst == null || validFirst == "") { 
    document.getElementById("firstError").innerHTML = "Required Field"; 
    document.getElementById("first").style.backgroundColor = "red"; 

} 
else if (validFirst.length < 2) {   
    document.getElementById("firstError").innerHTML = "Response too short"; 
    document.getElementById("first").style.backgroundColor = "red"; 

    } 
else if (validFirst !== null) {   
    document.getElementById("firstError").innerHTML = ""; 
    document.getElementById("first").style.backgroundColor = "white"; 

    } 

var validLast = document.forms["myForm"]["lname"].value; 
if (validLast == null || validLast == "") { 
    document.getElementById("lastError").innerHTML = "Required Field"; 
    document.getElementById("last").style.backgroundColor = "red"; 

} 
else if (validLast.length < 2) {   
    document.getElementById("lastError").innerHTML = "Response too short"; 
    document.getElementById("last").style.backgroundColor = "red"; 

    } 
else if (validLast !== null) {   
    document.getElementById("lastError").innerHTML = ""; 
    document.getElementById("last").style.backgroundColor = "white"; 

    } 
    var validEmail = document.forms["myForm"]["email"].value; 
if (validEmail == null || validEmail == "") { 
    document.getElementById("emailError").innerHTML = "Required Field"; 
    document.getElementById("email").style.backgroundColor = "red"; 

} 
else if (validEmail.length < 2) {   
    document.getElementById("emailError").innerHTML = "Response too short"; 
    document.getElementById("email").style.backgroundColor = "red"; 

    } 
else if (validEmail !== null) {   
    document.getElementById("emailError").innerHTML = ""; 
    document.getElementById("email").style.backgroundColor = "white"; 

    } 

    var validPhone = document.forms["myForm"]["phone"].value; 
if (validPhone == null || validPhone == "") { 
    document.getElementById("phoneError").innerHTML = "Required Field"; 
    document.getElementById("phone").style.backgroundColor = "red"; 

} 
else if (validEmail.length < 2) {   
    document.getElementById("phoneError").innerHTML = "Response too short"; 
    document.getElementById("phone").style.backgroundColor = "red"; 

    } 
else if (validEmail !== null) {   
    document.getElementById("phoneError").innerHTML = ""; 
    document.getElementById("phone").style.backgroundColor = "white"; 

    } 
    return false; 
    } 

</script> 
<head> 
<title>Form Validation</title> 
</head> 
<body> 

<form name="myForm" action="" onsubmit="return validateForm()" method="post"> 
<table> 
     <tr> 
      <td>First Name</td> 
      <td><input type="text" id="first" name="fname" onchange="validFirst()"></td> 
      <td><span id="firstError"></span></td> 
     </tr> 
     <tr> 
      <td>Last Name</td> 
      <td><input type="text" id="last" name="lname" onchange="validLast()"></td> 
       <td><span id="lastError"></span></td> 
     </tr> 

     <tr> 
      <td>Email</td> 
      <td><input type="text" id="email" name="email" onchange="validEmail()"></td> 
      <td><span id="emailError"></span></td> 
     </tr> 
     <tr> 
      <td>Phone</td> 
      <td><input type="text" id="phone" name="phone" onchange="validPhone()"></td> 
      <td><span id="phoneError"></span></td> 
     </tr> 
     <tr> 
      <td><input type="submit" value="Submit"></td> 
      <td></td> 
      <td></td> 
      </tr> 
</table> 

</body> 
</html>