2012-08-24 8 views
0

如果輸入字段與電子郵件和密碼條件匹配,我將向該字段添加「有效」類。我想啓用一個提交按鈕,如果兩個字段都有一個有效的類,一旦有效的類被添加,它仍然在那裏,即使輸入的輸入被完全刪除。使用jquery輸入字段驗證

要解決這個問題,我必須刪除有效的類或增加計數器時,該字段不爲空。我正在嘗試第二種情況,我正在檢查該類是否存在,以及該字段是否爲空。我沒有收到預期的效果用下面的代碼:爲有一個C.options.currentPage參考和count未定義

<input type="email" name="email" placeholder="Email" class="email required valid" id="aim_Email"> 
<input type="password" name="password" id="aim_Password" placeholder="Password" class="newpassword password required aimError" style=""> 


$('.required').live('blur', function (value) { 
    if ($(this).hasClass('valid') && $(this).val()) { 
    count++; 
    if (count > 1) { 
     alert('event fired'); 
     $(".submit").removeClass("disabled"); 
     $(C.options.currentPage).find('input[type="submit"]').removeAttr("disabled"); 
     $('.passwordHint').hide(); 
    } 
    } 
}); 
+0

您是否看過輸入屬性的html5語法?看到這個http://www.the-art-of-web.com/html/html5-form-validation/ – nikhil

回答

1

您需要在計數器爲空時遞減計數,您還需要限制輸入字段數內的計數。看看這個jsFiddle

0

的例子是不完整的。

就這樣說,我不確定你期待什麼,因爲它適合我。檢查jsFiddle here。我修改了一下你的代碼。

0

只需檢查每個關鍵事件。並使用toggleClass() ..因爲它允許在傳遞一個條件..所以如果真的把它添加類..如果虛假它消除類

$('.submit').prop('disabled',true); // <-- disable button on load 
$('.required').keyup(function() { // <-- bind keyup to both required inputs 
    $('.required').each(function(i, v) { // <-- loop through each required field 
      $(v).toggleClass('valid',$(v).val().trim().length > 0); // <-- add class if it has text 
    }); 
    $('.submit').prop('disabled', $('.valid').length != 2); // button disabled if there's not 2 valid fields 
});​ 

http://jsfiddle.net/nxLeJ/3/

+0

看起來像這樣的作品,讓我執行並確認。 – neelmeg