问题
Is there a way to hook into MVC3 unobtrusive remote validation on the client, before data is submitted to the remote method, and after a result is returned from the remote method?
I am trying to tell a remote validator to only perform validation if the submit button is clicked. The normal behavior is to validate several times, such as during keypress, blur etc. This is no good, as the validation method in question needs to call a non-idempotent operation. I need to make sure that method is only invoked if the user has clicked the submit button.
If I could hook into a before event, I could set a field in the form that flags the submit button as being clicked or not. However I would need to reset this flag after the remote method returns a validation result.
Any other suggestions? This is for validating a password using the ASP.NET Membership Provider's Membership.ValidateUser(string username, string password)
method. This method will increment the FailedPasswordAttemtCount
each time an invalid password is sent, so I don't want it to execute during blur, keypress, etc.
回答1:
You could override the default validation options for the current page:
$.validator.setDefaults({
onkeyup: false,
onfocusout: false,
onsubmit: true
});
回答2:
The solution above didn't resolve this for me. Somehow, the validator setting was not being set. So, I implemented the following and it worked:
$(function () {
var settings = $.data($('form')[0], 'validator').settings;
settings.onkeyup = false;
});
来源:https://stackoverflow.com/questions/10414244/event-hooks-for-mvc-unobtrusive-remote-validation