Search code examples
jqueryunobtrusive-validation

Disable validation for an element with jQuery Unobtrusive Validation


I am generating a list of elements on a page, and they all have validators attached to them. When I look in the HTML source, I see something like:

<input type="text" id="email" name="email" data-val-required="No valid email address!" data-val="true">

I need a way to dynamically enable/disable the validation for such an element. I tried to enable/disable the data-val attribute by setting it to false and then back to true. But it doesn't seem to response to that; the validation is always there!

Does anyone have any idea how I can enable/disable validators on certain fields dynamically?


Solution

  • I actually found a solution that fits my needs better. I can do the following:

    $(function() {
        var settngs = $.data($('form')[0], 'validator').settings;
        settngs.ignore = ".ignore";
    });
    

    And with that i can 'toggle' any element that i want by adding or removing the classname ignore from an element.