Enable/Disable Validator controls in ASP.Net
Sometimes, we will have requirements to enable/disable validator control for specific business conditions.
Enabling Disabling Validator control from JavaScript One way is to set the enabled flag of the validation control to false and make it disabled.
document.getElementById('RequiredFieldValidator2').enabled = false;
Again, setting it to true will enable it.
The other way will be calling inbuilt validation script's ValidatorEnable() method.
ValidatorEnable(document.getElementById('RequiredFieldValidator2'),false);
Calling this method with true in second argument will enable the validation control. This method will have its definition in the inbuilt validation script that gets downloaded to the client browser via web resource handler called WebResource.axd.
Enabling/Disabling a Validator control that belongs to a Validation Group
The below script will disable a validator control that belongs a validation group called "grp".
if(document.getElementById('RequiredFieldValidator2').validationGroup == "grp") { document.getElementById('RequiredFieldValidator2').enabled = false; }
Enabling/Disabling validator control from serverside We can enable or disable a validator control from server side by setting the validator control's EnableClientScript property to true/false.
To disable a validator control, RequiredFieldValidator2.EnableClientScript = false;
|