2012-11-02 19 views
1

我已經寫seprate規則jQuery的validate()命名爲login_auth,我想這條規則是不呼籲每個按鍵但我想其他所有規則被稱爲onkeyup。這意味着所有其他內置規則類似emailpassword應在每個密鑰上調用,但只應調用login_authonfocusoutblur如何使jQuery Validate()規則爲onkeyup爲false?

+0

請有人幫忙 – Akki

+2

如果您需要幫助,請顯示您的表單代碼... HTML和jQuery。 – Sparky

+0

這裏回答類似的問題http://stackoverflow.com/a/17096209/678338 –

回答

3

這是我最好的答案,直到你發佈你的實際的HTML和jQuery。

我知道沒有辦法選擇性地應用任何插件選項....而是它們會應用到插件的每個實例。

因此,我建議兩種形式,以便您可以擁有.validate()的兩個唯一實例,並在一個表單上應用onkeyup: false選項,同時允許onkeyup在其他表單上正常工作。

HTML:

<form id="authForm" action="#"> 
    login: <input type="text" name="login" /><br /> 
</form> 

<form id="Form" action="yourAction" method="yourMethod"> 
    email: <input type="text" name="email" /><br /> 
    password: <input type="password" name="password" /><br /> 
    <input type="submit" /> 
</form>​ 

的jQuery:

$(document).ready(function() { 

    $('#authForm').validate({ 
     onkeyup: false, 
     rules: { 
      login: { 
       login_auth: true, // your custom rule 
       required: true 
      } 
     } 
    }); 

    $('#Form').validate({ 
     rules: { 
      email: { 
       required: true, 
       email: true 
      }, 
      password: { 
       required: true 
      } 
     }, 
     submitHandler: function(form) { 
      if ($('#authForm').valid()) { 
       form.submit(); 
      } 
     }, 
     errorPlacement: function(error, element) { 
      $('#authForm').valid(); // force validation on first form when this form has error 
      error.insertAfter(element); // default error placement 
     } 
    }); 

});​ 

Working Demo:

http://jsfiddle.net/4Hkjk/5/

3
$.validator.setDefaults({ 
    onkeyup: function(element) { 
     if (element.name == '#emailid') { 
      return false; 
     } 
    } 
}); 

在一個表單中,如果您想停止某個特定ID的關鍵事件,請嘗試此操作。

相關問題