How can I add callbacks to jquery validation (when used in MVC 2)

南楼画角 提交于 2019-12-03 12:49:26

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!