开发者

How to set errorContainer in jQuery validation dynamically?

开发者 https://www.devze.com 2022-12-16 08:19 出处:网络
Can I dynamically set errorContainer in jQuery validation, which means showing different error container base on the button clicked.

Can I dynamically set errorContainer in jQuery validation, which means showing different error container base on the button clicked.

This is what I am trying to do, but seems doesn't work:

     $('#b1').click(function(evt) {
        $('#form1').validate().resetForm();
        validator.settings.errorContainer = $('#m1');
        validator.settings.errorLabelContainer = $('ul', $('#m1'));

        ValidateAndSubmit(evt)
        if (evt.isDefaultPrevented())
            return false;

        alert("b1 clicked");
    });

    $('#b2').click(function(evt) {
        $('#form1').validate().resetForm();
        validator.settings.errorContainer = $('#m2');
        validator.settings.errorLabelContainer = $('ul', $('#m2'));         

        ValidateAndSubmit(evt);
        if (evt.isDefaultPrevented())
            return false;

        alert("b2 clicked");
    });

the reason I'm doing this, because I'm using asp.net webform all my module inside one form tag, so I can't define another form inside existing form and set validation for each form, and I can only have 开发者_Go百科one validation bind to the form.

I am using encosia's solution to simulate validation group using jQuery validation for sub-form under one form tag which is working perfectly, but in some case sub-forms under different tabs, so I want to showing the error container in each tab.

I have created a simple sample to explain what i'm trying to do sample here


If you already download the zip file, you will find a live site example in it. My favorite is the RTM example. The page have two type of form field, the one inside the table, and outside the table. For the field inside the table, the error message will appear in the table's column beside the column of field with error. For the field outside table, the error message will appear right beside the form field.

The form validation library is clearly lack of proper documentation. I learn all cool trick from the sample live site.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号