2013-06-23 125 views
0

這很像問題所暗示的,我有一個帶JS驗證的HTML表單。現在,我仍在使用驗證調試細節,但問題在於,onSubmit函數會在應該出現時觸發錯誤,但表單仍然提交 - 我不知道爲什麼。我已經檢查了StackOverflow中的一百萬和一個類似的問題,但沒有一個與我的原因相同 - 我已經檢查並檢查並檢查。如果有人能幫忙,我會非常感激。JS表單驗證失敗,但表單仍然提交?

此外,我知道我的代碼可以縮短,但我會這樣做,當我解決這個問題。

形式:

<form name="registerForm" method="post" action="index.php" onSubmit="return validateForm()"> 

      <table class="formTable" > 
      <tr> 
       <td><i class="smallprint">* denotes a required field.</i></td> 
      </tr> 
      <tr> 
       <td> 
        <input type="text" maxlength="32" width="32" name="regTxtUsrName"> 
       </td> 
       <td> 
        User Name* <i class="smallprint"> &nbsp; between 6 and 32 characters (letters, numbers and underscores only)</i><br /> 
        <b class="error" id="userNameError">Error: Please enter a user name between 6 and 32 characters, using letters, numbers and underscores</b> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="text" maxlength="32" width="32" name="regTxtFName"> 
       </td> 
       <td> 
        First Name*<br /> 
        <b class="error" id="fNameError">Error: Please enter your first name</b> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="text" maxlength="32" width="32" name="regTxtSName"> 
       </td> 
       <td> 
        Surname*<br /> 
        <b class="error" id="sNameError">Error: Please enter your surname</b> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="text" maxlength="32" width="32" name="regTxtEmail"> 
       </td> 
       <td> 
        Email* &nbsp; <i class="smallprint">Please use a valid email address, it will be used to validate your account</i><br /> 
        <b class="error" id="emailError1">Error: Please enter an email address<br /></b> 
        <b class="error" id="emailError2">Error: This is not a valid email address</b> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="text" maxlength="32" width="32" name="regTxtEmailConf"> 
       </td> 
       <td> 
        Confirm Email*<br /> 
        <b class="error" id="emailConfError">Error: Both email addresses must match</b> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="password" maxlength="32" width="32" name="regTxtPassword"> 
       </td> 
       <td> 
        Password* &nbsp; <i class="smallprint">Between 6 and 32 characters, using at least one letter and one number</i><br /> 
        <b class="error" id="passwordError">Error: Please enter a valid password</b> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input type="password" maxlength="32" width="32" name="regTxtPasswordConf"> 
       </td> 
       <td> 
        Confirm Password*<br /> 
        <b class="error" id="passwordConfError">Error: Both email passwords must match</b> 
       </td> 
      </tr> 
      </table> 
      <br /> 
      <div class="textCenter"> 
       <input type="checkbox" class="center" name="regChkTos"> - Check this box if you agree to the Terms of Service. You must agree in order to regster. 
       <br /> 
       <br /> 
       <input type="submit" name="regSubmit" value="Register"> 
      </div> 

     </form> 

JS:

<script type="text/javascript"> 
    function validateForm() 
    { 
     var noError = true; 
     if (!validateUserName()) 
     { 
      noError = false; 
     } 
     if (!validateFName()) 
     { 
      noError = false; 
     } 
     if (!validateSName()) 
     { 
      noError = false; 
     } 
     if (!validateEmail()) 
     { 
      noError = false; 
     } 
     if (!validateConfirmEmail()) 
     { 
      noError = false; 
     } 
     if (!validatePassword()) 
     { 
      noError = false; 
     } 
     if (!validateConfirmPassword()) 
     { 
      noError = false; 
     } 
     return noError; 
    } 

    function validateUserName() 
    { 
     var userName = document.forms["registerForm"]["regTxtUsrName"]; 
     var regex = /^\w+$/; 
     if (userName.value==null || userName.value=="" || userName.value.length < 6 || !regex.test(userName.value)) 
     { 
      userName.style.border = "2px solid red"; 
      document.getElementById('userNameError').style.display="inline"; 
      return false; 
     } 
     else 
     { 
      userName.style.border = "2px solid #0f0"; 
      document.getElementById('userNameError').style.display="none"; 
      return true; 
     } 
    } 

    function validateFName() 
    { 
     var name = document.forms['registerForm']['regTxtFName']; 
     if (name.value == null || name.value == '') 
     { 
      name.style.border = "2px solid red"; 
      document.getElementById('fNameError').style.display="inline"; 
      return false; 
     } 
     else 
     { 
      name.style.border = "2px solid #0f0"; 
      document.getElementById('fNameError').style.display="none"; 
      return true; 
     } 
    } 

    function validateSName() 
    { 
     var name = document.forms['registerForm']['regTxtSName']; 
     if (name.value == null || name.value == '') 
     { 
      name.style.border = "2px solid red"; 
      document.getElementById('sNameError').style.display="inline"; 
      return false; 
     } 
     else 
     { 
      name.style.border = "2px solid #0f0"; 
      document.getElementById('sNameError').style.display="none"; 
      return true; 
     } 
    } 

    function validateEmail() 
    { 
     noError = true; 
     var email = document.forms['registerForm']['regTxtEmail']; 
     var atpos=email.value.indexOf("@"); 
     var dotpos=email.value.lastIndexOf("."); 
     if (email.value == null || email.value == '') 
     { 
      email.style.border = '2px solid red'; 
      document.getElementById('emailError1').style.display='inline'; 
      noError = false; 
     } 
     else 
     { 
      document.getElementById('emailError1').style.display='none'; 
      if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.value.length) 
      { 
       email.style.border = '2px solid red'; 
       document.getElementById('emailError2').style.display='inline'; 
       noError = false; 
      } 
      else 
      { 
       email.style.border = '2px solid #0f0'; 
       document.getElementById('emailError2').style.display='none'; 
      } 
     } 
     return noError 
    } 

    function validateConfirmEmail() 
    { 
     var email = document.forms['registerForm']['regTxtEmail']; 
     var emailConf = document.forms['registerForm']['regTxtEmailConf']; 
     if (email.value != emailConf.value) 
     { 
      emailConf.style.border = '2px solid red'; 
      document.getElementById('emailConfError').style.display = 'inline'; 
      return false 
     } 
     else 
     { 
      emailConf.style.border = '2px solid 0f0'; 
      document.getElementById('emailConfError').style.display = 'none'; 
      return true 
     } 
    } 

    function validatePassword() 
    { 
     var password = document.forms['registerForm']['regTxtPassword']; 
     if (password.value == null || password.value == '' || password.value.length < 6 || password.value.search(/\d/) == -1 || password.value.search(/[A-z]/) == -1) 
     { 
      password.style.border = '2px solid red'; 
      document.getElementById('passwordError').style.display = 'inline'; 
      return false; 
     } 
     else 
     { 
      password.style.border = '2px solid 0f0'; 
      document.getElementById('passwordError').style.display = 'none'; 
      return true; 
     } 
    } 

    function validatePasswordConf() 
    { 
     var password = document.forms['registerForm']['regTxtPassword']; 
     var passwordConf = document.forms['registerForm']['regTxtPasswordConf']; 
     if (password.value != passwordConf.value) 
     { 
      passwordConf.style.border = '2px solid red'; 
      document.getElementById('passwordConfError').style.display = 'inline'; 
      return false; 
     } 
     else 
     { 
      passwordConf.style.border = '2px solid 0f0'; 
      document.getElementById('passwordConfError').style.display = 'none'; 
      return true; 
     } 
    } 
    </script> 

是的,這個劇本就是形式HTML網頁上。我已經將每個字段留空,點擊提交,這些錯誤暫時彈出,然後調用action="index.php"

感謝您的幫助。

+0

順便說一句,測試'userName.value == null'永遠不會是真的,因爲表單控件的值是一個字符串,並且一個字符串(即使是一個空字符串)也不會「等於」null(參見[abstract相等算法](http://ecma-international.org/ecma-262/5.1/#sec-11.9.3))。 – RobG

+0

謝謝你的擡頭。這只是我學到的東西(我認爲在W3C學校),而且我從此就做到了這一點,但這足以阻止我編寫多餘的代碼,謝謝! –

+0

您應該將代碼減少到顯示發佈前的行爲的最小值。這樣做,你會經常發現你的錯誤。否則,回到基本的一個字段和一個驗證。當你得到這個工作時,一個接一個地添加更多的功能和領域。那麼當你遇到問題時,你就會知道它在哪裏。 – RobG

回答

0

首先你需要知道錯誤在哪裏。 爲此,請在每個函數上使用console.log。 之後,您的代碼可以使用更好的算法和正則表達式進行優化。 要知道問題出在哪裏檢查每個函數的每個返回。 對主函數我會做財產以後這樣的:

function validateForm(){ 
    if (validateUserName() && validateFName() && validateSName() && validateEmail() && validateConfirmEmail() && validatePassword() && validateConfirmPassword()){ 
     return true; 
    } 
    return false; 
} 

編輯:檢查你的主函數的值了。 您是否有直接鏈接到此頁面?

+0

這是一個更整潔的版本,但它並沒有幫助這個問題,當然。我不知道如何使用console.bug,我剛剛安裝了Firebug來嘗試它,但我無法弄清楚它。這裏有一個鏈接: http://www.kallumtanton.x10xm/mvphotography/register.php –

+0

ReferenceError:validateConfirmPassword沒有定義 你需要使函數來定義它,它會好的 – daarksim

+0

非常感謝!我不敢相信這是一個重命名的錯誤,我確信我得到了所有這些。那麼,還有另外一個關於這裏的其他一萬個基本錯誤問題。再次感謝,我很抱歉浪費你的時間:redface: –