我想我想通了這一點。我使用addMethod來創建一個我稱之爲「isExtrabuxMember」的規則,而不是使用「遠程」選項。我還創建了一些全局變量,並使用綁定到#email字段更改的ajax來檢查提供的電子郵件地址是否屬於任何現有的Extrabux成員。
請評論,如果這可以幫助你或如果你有更好的想法。
我現在有這樣的「驗證」插件調用上面:
jQuery.validator.addMethod("isExtrabuxMember", function(value, element) {
var emailRemoteFuncResult = checkSigninEmailAddressResult === null ? true : checkSigninEmailAddressResult;
return emailRemoteFuncResult;
});
var checkSigninEmailAddressResult = null;
var emailXhrCheck;
$('#email').bind('change keyup', function(){
checkSigninEmailAddressResult = null;
if(emailXhrCheck){
emailXhrCheck.abort();
}
emailXhrCheck = $.ajax({
url: '/users/check-signin-email-address',
type: "post",
async: true,
data: {
emailAddress: function() {
return $("#email").val();
}
},
success: function(data){
checkSigninEmailAddressResult = data;
$("#email").valid();
}
});
});
$('#loginForm').submit(function(){
if(emailXhrCheck){
emailXhrCheck.abort();
}
});
然後內的「驗證」插件調用:
rules: {
email: {
required: true,
email: true,
isExtrabuxMember: true
},
password: {
required: true,
minlength: 4
}
},
messages: {
email: {
isExtrabuxMember: function(){
var currentEmail = $('#email').val();
return $.validator.format('<b>{0}<\/b> does not yet have an Extrabux account. <a href="\/users\/register?emailAddress={0}">Sign up?<\/a>', currentEmail);
}
},
password: {
required: "Oops, you forgot to enter your password!"
}
}
你使用jQuery的AJAX功能以dB爲單位來檢查電子郵件? –
這可能有所幫助:http://stackoverflow.com/questions/446594/abort-ajax-requests-using-jquery – greener
我使用我上面顯示的代碼。它將字符串電子郵件地址發佈到www.extrabux.com/users/check-signin-email-address並收到json真/假結果。請參閱http://docs.jquery.com/Plugins/Validation/Methods/remote。謝謝! – Ryan