2012-08-09 92 views
1

我有以下JS代碼:表單驗證失敗,但仍提出

function checkFull(id) { 
    var input = $('#' + id).val(); 
    if (input == "") { 
     return false; 
    } 
    else { 
     return true; 
    } 
} 

function checkSame(id1,id2) { 
    var input1 = $('#' + id1).val();  
    var input2 = $('#' + id2).val(); 
    if (input1 == input2) { 
     return true; 
    } 
    else { 
     return false; 
    } 
} 
function showErrors() { 
    if (!checkFull('username')) { 
     $('#userfield').show(); 
    } 
    else { 
     $('#userfield').hide(); 
    } 

    if (!checkFull('email')) { 
     $('#notsame').show(); 
    } 
    else { 
     $('#notsame').hide(); 
    } 

    if (!checkFull('confemail')) { 
     $('#notsame2').show(); 
    } 
    else { 
     $('#notsame2').hide(); 
    } 

    if (!checkFull('espncode')) { 
     $('#nocode').show();  
    } 
    else { 
     $('#nocode').hide();  
    } 

    if (notSame('email','confemail')) { 
     $('#notsame2').show(); 
     $('#notsame').show(); 
    } 
    else { 
     $('#notsame2').hide(); 
     $('#notsame').hide(); 
    } 
} 
function valform() { 
    showErrors(); 
    if (checkFull('username') && checkFull('email') && checkFull('confemail') && checkSame('email','confemail') && checkFull('espncode')) { 
     form.submit(); 
     alert('success'); 
    } 
    else { 

     return false; 
    } 
} 

而下面的形式:

<form method="post" action="forms/post.asp" onsubmit="valform();return false"> 
      <strong>Poker Username</strong> <span id="userfield" style="display:none;color:#F00">*</span><br /> 
      <input type="text" name="username" id="username" style="width:230px;" /> 
      <br /><br /> 
      <div class="vert"> 
       <strong>Email Address</strong> <span id="notsame" style="display:none;color:#F00">*</span><br /> 
       <input type="text" name="email" id="email" style="width:230px;" /> 
      </div> 
      <div class="vert2"> 
       <strong>Confirm Email Address</strong> <span id="notsame2" style="display:none;color:#F00">*</span><br /> 
       <input type="text" name="confemail" id="confemail" style="width:230px;" /> 
      </div> 
      <div style="clear:both;"></div> 
      <div class="espn"> 
       <div class="txt"><strong>Enter the ESPN special code</strong></div> 
       <input type="text" name="espncode" id="espncode" style="width:230px;" /> 
      </div> 
      <div id="nocode" style="display:none"> 
       You need to enter the code above 
      </div> 
      <div class="tc"> 
       <input type="checkbox" id="agree" name="agree" /> <strong>You agree to the terms and conditions of this sweepstake. Only one entry per person per sweepstake period. Any additional entries will be disqualified.</strong> 
      </div> 
      <br /> 
      <div class="submit"> 
       <input type="image" src="submit_BTN.png" /> 
      </div> 
     </form> 

即使我離開空白表單,當我按下提交它會去通過。這是爲什麼?

+0

有沒有您的驗證工作? – Prateek 2012-08-09 08:30:00

+0

@pKs - yes運行showErrors()時顯示的'錯誤'正在瞬間顯示,然後表單提交 – user838437 2012-08-09 08:30:46

+0

您的前兩個函數可以大大簡化爲[this](https ://gist.github.com/c6945009d34c9e1842c7) – elclanrs 2012-08-09 08:32:03

回答

3

您需要從您的驗證函數返回submit處理程序中的值:

onsubmit="valform();return false" 

應該是:

onsubmit="return valform();" 

每當valform返回false這將防止從形式提交。

順便說一句,form.submit()呼叫您的valform函數是沒有意義的。如果函數不返回false,它將被提交。

if (/*validity checks pass*/) { 
    alert('success'); 
} 
else { 
    return false; 
} 

但是,通過您的代碼with this fiddle找我注意到有一個錯誤:

ReferenceError: Can't find variable: notSame

如果您解決您的函數的名稱,將工作。您應該使用可用的開發人員工具來調試此類小錯誤。 Firebug for Firefox和Chrome/Safari隨附了一個內置的網絡檢查器。Opera也有Dragonfly。

+0

謝謝,我剛剛嘗試過,它仍然提交表單..任何想法? – user838437 2012-08-09 08:37:45

+0

錯誤可能在您的驗證中。你確定它做你打算做的事情嗎? – 2012-08-09 08:39:28

+0

我刪除了form.submit()從應該執行的部分,當窗體驗證,並只留下一個「警報(」成功「);''那裏。當我提交表單時,我沒有得到提示,但表單仍然提交,所以從我收集的內容中返回false,但仍然提交表單。 – user838437 2012-08-09 08:40:55

1

可能的原因:

  • onsubmit="valform();return false"變化onsubmit="valform();"形式。

嘗試如果仍然沒有工作,然後告訴。