Search code examples
asp.net.netrequiredfieldvalidator

overriding RequriedFieldValidator on client side?


Is it possible to override RequiredFieldValidator's functionality on client side ? I want to enhance it and use for all controls in page. Please guide.


Solution

  • To do this, you have to create a class that inherits from RequiredFieldValidator an create instances of this custom server control and add to your page. Actually, I think you could loop in all controls and change your modifications, something like this:

    foreach(Control control in Page.Controls)
    {
       if (control is RequiredFieldValidator)
       {
          RequiredFieldValidator rfv = (RequiredFieldValidator) control;
          // do your customizations for all RequiredFieldValidator
          rfv.ErrorMessage = "...";
          rfv.SetFocusOnError = true;
       }
    }