Customising event delegates in the jQuery validati

2019-06-17 08:51发布

I am currently setting up the jQuery validation plug-in for use in our project.

By default, there are some events automatically set up for handling. I.e. focus in/out, key up events on all inputs fire validation. I want it to only fire when the submit button is clicked.

This functionality seems to be in-built into the plug-in, which is making it difficult to do this (without modifying the plug-in code, Not What I Want To Do).

I have found the eventDelegate function calls in the plugin code prototype method:

        $(this.currentForm)
            .validateDelegate(":text, :password, :file, select, textarea", "focusin focusout keyup", delegate)
            .validateDelegate(":radio, :checkbox, select, option", "click", delegate);

When I remove these lines from the plug-in I get my result, however I would much rather do something Outside the plug-in to achieve this.

Can anybody please help me? If you need any more details, please let me know. I have searched google with little success.

Thanks

EDIT: I am basically trying to validate the form Only when the submit event is fired. By default, the plug-in validates every time focus is lost in an input control.

1条回答
Root(大扎)
2楼-- · 2019-06-17 09:29

Found the answer. It was (hidden?) away as part of the options in the validate method.

See the options onfocusout etc on this page: http://docs.jquery.com/Plugins/Validation/validate#options

which I can set to false.

Here is my code which sets up my validator (hopefully others will find this useful):

$(document).ready(function() {
    $("form").each(function() {
        $(this).validate({
            validateDelegate: function() { },
            onsubmit: true,
            onkeydown: false,
            onkeyup: false,
            onfocusin: false,
            onfocusout: false,

            errorContainer: "#PanelError",
            errorLabelContainer: "#PanelError ul",
            wrapper: "li",
            ignoreTitle: true,
            errorClass: "Error",

            highlight: function(element, errorClass, validClass) {
                $(element).addClass(errorClass).removeClass(validClass);
                $(element.form).find("#" + element.id)
                        .addClass(errorClass);
            },
            unhighlight: function(element, errorClass, validClass) {
                $(element).removeClass(errorClass).addClass(validClass);
                $(element.form).find("#" + element.id)
                        .removeClass(errorClass);
            }
        });
    });
});
查看更多
登录 后发表回答