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

前端 未结 1 661
无人及你
无人及你 2021-02-15 15:54

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,

相关标签:
1条回答
  • 2021-02-15 16:21

    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.

    0 讨论(0)
提交回复
热议问题