我如何添加回调jQuery验证(在MVC 2使用时) [英] How can I add callbacks to jquery validation (when used in MVC 2)
问题描述
我使用jQuery在我的MVC2 web应用程序验证(<一个href=\"http://stackoverflow.com/questions/2485326/jquery-validation-without-requiring-ms-scripts-in-asp-net-mvc2-project\">as这里 描述的),我想起来接线的jQuery验证插件支持一些回调,像<一个href=\"http://docs.jquery.com/Plugins/Validation/validate#toptions\">invalidHandler,等等。
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.
我可以手动编辑<一个href=\"http://$c$c.google.com/p/productinfo/source/browse/trunk/src/ProductInfoMVC/Scripts/MicrosoftMvcJQueryValidation.js?spec=svn10&r=10\">MicrosoftMvcJQueryValidation.js并加入我的回调(在__MVC_EnableClientValidation,在选项变量),但我一直在寻找一个更好的方法,因为该文件被反复使用,我不希望有创建一次性拷贝。
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.
到invalidHandler(等)回调手动添加到表单验证,会是正是我需要的一种方式。显然,这通常会通过选项主叫首次验证()时进行,但由于微软控制该特定的部分,即心不是一个选项。
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.
推荐答案
从<一个href=\"http://www.herikstad.net/2010/04/add-invalidhandler-after-jquery.html\">Herikstad.net:
如果你有你需要的问题
添加的选项 invalidHandler
来
您jqueryValidate(jQuery验证
插件)已经初始化之后,
这是它是如何做到:
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: {}
});
});
请注意:invalidHandler将被调用
当表单验证失败的
提交(例如值的字段
丢失或这样)。
Note: invalidHandler will be called when validation of form fails on submit (e.g. values for a field is missing or such).
这可能会为其他选项的工作
在jqueryValidate插件,但我不
一定要使用哪个属性。我发现
属性绑定到在
jquery.validate.js文件,你可能
想看看那里。
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.
这篇关于我如何添加回调jQuery验证(在MVC 2使用时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!