How can I add callbacks to jquery validation (when used in MVC 2)
I'm using jquery for validation in my MVC2 web app (as described here) and I'd like to wire up some callbacks that the jquery validation plugin supports, like invalidHandler, etc.
I can manually edit the MicrosoftMvcJQueryValidation.js and add my callbacks (in __MVC_EnableClientValidation, in the options variable) but I was looking for a better approach since that file is used repeatedly and I don't want to开发者_开发知识库 have to create one-off copies.
A way to manually add an invalidHandler (etc) callback to the form validation, would be exactly what I need. Obviously this would normally be done via the options when calling validate() for the first time, but since Microsoft controls that particular part, that isnt an option.
From Herikstad.net:
If you have a problem where you need to add the option
invalidHandler
to your jqueryValidate (jQuery Validation Plugin) after it has been initialized, this is how it can be done:$(document).ready(function(){ $("#contactForm").bind('invalid-form.validate', function(form, validator) { alert('validation failed!'); } ); });
Regularly you would add this on initialization:
$(document).ready(function(){ $('#contactForm').validate({ invalidHandler: function(form, validator) { alert('validation failed!'); }, rules: {} }); });
Note: invalidHandler will be called when validation of form fails on submit (e.g. values for a field is missing or such).
This might work for other options of the jqueryValidate plugin, but I'm not sure which property to use. I found the property to bind to in the jquery.validate.js file, you might want to look there.
精彩评论