JQuery disable / re-enable validators on page

When I need to disable / enable the validator again, I usually use jQuery code as follows:

ValidatorEnable($('[id*=DueDateRequiredValidator]')[0], false); 

and turn it back on:

  ValidatorEnable($('[id*=DueDateRequiredValidator]')[0], true); var validator = $('[id*=DueDateRequiredValidator]')[0]; validator.isvalid = true; ValidatorUpdateDisplay(validator); 

And it works. But now I need to disable / re-enable a large number of validators, but only a subset of validators, not entire sets on the page. There are too many of them to refer to each validator on "id".

I tried something like this, but it didn’t work, I don’t know why:

  $.each(Page_Validators, function(index, validator) { if ( validator.CssClass == "noinjuries" ) { ValidatorEnable(validator, false); } }); 

What is the best way to do this, if any?

+4
source share
2 answers

Yes try first

if ($(validator).hasClass('noinjuries')) { /* disable validator */ }

0
source

I found the following javascript code on a blog , it uses ValidationGroup to switch validation controls, but you can just as easily remove if (Page_Validators[i].validationGroup == validationGroupName) and toggle the entire set of validators.

After calling the "ValidatorEnable" method, you will need to add your own activation code.

 function HasPageValidators() { var hasValidators = false; try { if (Page_Validators.length > 0) { hasValidators = true; } } catch (error) { } return hasValidators; } function ValidationGroupEnable(validationGroupName, isEnable) { if (HasPageValidators()) { for (i = 0; i < Page_Validators.length; i++) { if (Page_Validators[i].validationGroup == validationGroupName) { ValidatorEnable($('[id*=DueDateRequiredValidator]')[0], true); } } } } 

Edit: Victor, based on your comment, I think you can get the same results by adding attributes to your controls. This would group them without using ValidationGroups, and you can access attributes from JavaScript.

In your code behind, you need to set the attributes for the Validation controls as follows:

 SampleValidator1.Attributes.Add("myGroupName", "sample1"); SampleValidator2.Attributes.Add("myGroupName", "sample2"); 

Then, in the Javascript function, instead of this line, `if (Page_Validators [i] .validationGroup == validationGroupName) {', use this:

 if (Page_Validators[i].getAttribute('myClass') == 'sample1') { 

This should get what you want without groups, but sorry - you will need to set the attribute


Last edit: I made it harder than necessary. I have not tried, but if you want to get the CSS class of an object in JavaScript, this is .className , not .class . Try this before any of my previous suggestions.

0
source

Source: https://habr.com/ru/post/1332810/


All Articles