2013-11-09 43 views
-1

好吧我正在盡我所能通過測試和實踐來學習jQuery。我知道有很多完美的庫來驗證表單,但我想至少現在就自己學習! 我在this Link中有一個表單,我在驗證表單時出錯。正如你可以看到我有兩個簡單的輸入電子郵件和用戶名現在我想要做的是檢查整個輸入在enter image description here第一步是看看是否有任何空投入或沒有,如果是這樣jquery會產生錯誤消息(這是做現在)enter image description here然後,我想添加一些字段驗證器來檢查所有輸入,在這裏我得到的問題
首先,代碼實際上沒有驗證輸入,也是將錯誤消息添加到另一個輸入好!最後,即使在輸入正確的輸入之後,也不會提交!另一個與表單驗證問題

這裏是我的代碼有:

$(document).ready(function() { 
var email = $('form #email').val(); 
$('#myform').submit(function() { 
     var abort = false; 
     $("div.err").remove(); 
     $(':input[required]').each(function() { 
      if ($(this).val()==='') { 
       $(this).parent().after('<div class="err">This is a Requierd Field</div>'); 
       abort = true; 
      } 
      else{ 
       if (!email.match(/^([a-z0-9._-][email protected][a-z0-9._-]+\.[a-z]{2,4}$)/i)) { 
       $(this).parent().after('<div class="err">Email Not Match</div>'); 
       abort = true;           
       } 
      } 
     }); 
     if (abort) { return false; } else { return true; 
     } 
    }) 
}); 

回答

0

要解決 '電子郵件不匹配' 問題,改變這一點 -

else{ 
    if (!email.match(/^([a-z0-9._-][email protected][a-z0-9._-]+\.[a-z]{2,4}$)/i)) { 
    $(this).parent().after('<div class="err">Email Not Match</div>'); 
    abort = true;           
    } 
} 

爲了這一點 -

else{ 
    if (!email.match(/^([a-z0-9._-][email protected][a-z0-9._-]+\.[a-z]{2,4}$)/i)) { 
    $(email).parent().after('<div class="err">Email Not Match</div>'); 
    abort = true;           
    } 
}