HOW TO: Display Required Field Validators at Page Load
A short snippet to force ASP.NET RequiredFieldValidators to run on page load, so required-field asterisks show up before the user submits.
Donn Felker
2007.07.20 · 1 min read
Sometimes when a page loads you’ll want the user to be able to see the which fields are required. This is normally done by presenting an asterisk next to the fields that need to be populated/selected.
The RequiredFieldValidator does not automatically perform validation upon load, but here’s some code that will allow you to do so:
I usually put this in the OnPreRender event handler as at that point everything is done processing on the page.
foreach (IValidatorvalidatorin Validators)
{
if (validator isRequiredFieldValidator)
{
validator.Validate();
}
}
This will ensure that the page’s required field validators are validated when the page loads. Therefore giving the user the insight into the required fields.
Written by Donn Felker
I've spent 25+ years shipping software, writing books, and running my own companies. I write about thinking clearly, working for yourself, and doing real work while AI rewrites the rules. New essays land here every week.