Thursday, March 25, 2010

Enable/Disable Asp.net validatiors using JavaScript

ASP.NET provides predefined validators for controls in ASP.NET pages:
  • RequiredFieldValidator
  • RegularExpressionValidator
  • ...

These validation controls offer the server-side property "Enabled" to be able to Enable/Disable them from server-side code. But what if we want to Enable/Disable them in client-side code (Javascript): Then there is a predefined JS method: ValidatorEnable(ValidatorId, Boolean); which allows us to do so.

To enable a control we would do like:
ValidatorEnable(document.getElementById('<%= validatorUserName.ClientID %>'), true);

And to disabe it:
ValidatorEnable(document.getElementById('<%= validatorUserName.ClientID %>'), false);

Couldn't it be easier?

0 comments:

Post a Comment