2016-10-10 119 views
-1

嘿傢伙真的基本問題在這裏我遇到了麻煩。這是我第一次從頭開始編碼驗證,無法弄清楚爲什麼他們不工作。當我點擊我的提交按鈕時,它只是轉發到「您的文件未找到」頁面。它根本不承認我的驗證者。通常在驗證器上輸入,以及如何改進或修復此問題將不勝感激。簡單的驗證器不工作,新的自定義驗證程序

<DOCTYPE!> 
 
<html> 
 
    <head> 
 
     <script src="gen_validatorv4.js" type="text/javascript"></script> 
 

 
     <link rel="stylesheet" type="text/css" href="style.css"> 
 

 
     <body> 
 
     <div id= "videoGameCircuit"> 
 
      <img src="Images/vg.jpg" width="900" height="400" alt="video game circuit" /> 
 
     </div> 
 
     <div> 
 
      <form action="myForm" onsubmit="return validateForm()" method="post"> 
 
      Name: <input type="text" name="name" value="" size="25" maxlength="50" /><br> 
 
      Email: <input type="text" name="email" value="" size="25" maxlength="50" /><br> 
 
      <input type="submit" name="submit" value="sign me up!" /> 
 
      </form> 
 
      <script type="text/javascript"> 
 
      var frmvalidator = new Validator("myForm"); 
 
      //where myform is the name/id of your form 
 

 
      var ele = myForm; ele.addEventListener("submit", function(e){e.preventDefault(); 
 
     
 

 
      frmvalidator.EnableOnPageErrorDisplaySingleBox(); 
 
      frmvalidator.EnableMsgsTogether(); 
 

 

 
      frmvalidator.addValidation("name","req","Please enter your name"); 
 
      frmvalidator.addValidation("name","maxlen=20", \t "Max length for name is 20"); 
 
      frmvalidator.addValidation("name","alpha_s","Name can contain alphabetic chars only"); 
 
      </script> 
 
     </div> 
 
     </body> 
 
    </head> 
 
</html>

+0

http://stackoverflow.com/questions/27918843/javascript-validate-form-b​​efore-submit .. http://stackoverflow.com/questions/27918843/javascript-validate-form-b​​efore-submit –

+0

仔細看看那裏。您有表單提交問題。您需要添加一個偵聽器來阻止默認提交。然後你可以運行你的驗證和東西,並在以後提交,如果一切都會好的。這裏是偵聽器的例子http://stackoverflow.com/questions/7410063/how-can-i-listen-to-the-form-submit-event-in-javascript ..也validateForm()方法?你有嗎? –

+0

@MykolaBorysyuk仍然是一個問題。 –

回答

0

在你的情況,你需要做驗證之前停止形式的默認submiting。

所以在你的情況下是這樣的。

var frmvalidator = new Validator("myForm"); 
     //where myform is the name/id of your form 
     frmvalidator.EnableOnPageErrorDisplaySingleBox(); 
     frmvalidator.EnableMsgsTogether(); 
     frmvalidator.addValidation("name","req","Please enter your name"); 
     frmvalidator.addValidation("name","maxlen=20", "Max length for name is 20"); 
     frmvalidator.addValidation("name","alpha_s","Name can contain alphabetic chars only"); 
//add id to form like id="myForm" 
document.querySelector("#myForm").addEventListener("submit", function(e){ 
    e.preventDefault(); //stop form from submitting 
    //run your validation code 
    var valid = frmvalidator.valid();//your custom validation method 
    if(!valid){ 
     //alert to user 
    } 
    else { 
     //submit form 
     e.submit(); 
    } 
}); 

希望這會有所幫助。

+0

沒有運氣。不過謝謝。 –

+0

你確定你清除onsubmit =「return validateForm()」並添加id來形成? –

+0

我不知道該怎麼做。這實際上是我在Web開發中查看自定義事件處理的第一個早晨。 –