Validation plugin - Auto validate multiple forms on a page

前端 未结 1 610
暖寄归人
暖寄归人 2020-12-09 09:42

Currently I need to validate every form like this:

    $(document).ready(function () {
        $(\'#admin_settings_general\').validate({
            rules: {         


        
相关标签:
1条回答
  • 2020-12-09 10:05

    Quote OP:

    "I want that it automaticly validate the forms for every element with the required tag."

    Quote OP comment:

    "i have to call $('#admin_settings_general').validate() for each of the forms currently. How can i call it without limiting it to one form?"


    To properly initialize .validate() on all forms on a page, use a common selector such as the form tag itself (or a class). Since you cannot attach .validate() to any jQuery selector that represents multiple form elements, use a jQuery .each(). This is simply how this plugins methods were designed.

    $(document).ready(function() {
    
        $('form').each(function() {  // attach to all form elements on page
            $(this).validate({       // initialize plugin on each form
                // global options for plugin
            });
        });
    
    });
    

    Working DEMO: http://jsfiddle.net/6Fs9y/

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