Share via


How to: Add and Configure a CustomValidator Control

Use the CustomValidator control to perform customized validation on input data in TextBox and SelectionList controls.

Note

If the user leaves the targeted control blank, it passes the custom validation test. To force the user to enter a value, add a RequiredFieldValidator control as well. For details, see How to: Add and Configure a RequiredFieldValidator Control.

To validate with a CustomValidator control

  1. Add a CustomValidator control to the form that contains the target control.

  2. Set the ControlToValidate property to the ID of the control to validate.

  3. Set the ErrorMessage property to a string to display if the target control fails validation.

    Note

    If you enter a string into the Text property, the CustomValidator control displays that text when an error occurs. If the Text property is left empty, the CustomValidator control displays the string defined in the ErrorMessage property.

  4. Switch to Design view, and then double-click the CustomValidator control.

    Visual Studio opens the code editor with an empty validation method.

  5. Create the handler. For details, see Introduction to the CustomValidator Control.

See Also

Tasks

How to: Add and Configure a CompareValidator Control

How to: Add and Configure a RangeValidator Control

How to: Add and Configure a RegularExpressionValidator Control

How to: Add and Configure a RequiredFieldValidator Control

How to: Add and Configure a ValidationSummary Control

How to: Test Validity Programmatically for ASP.NET Server Controls

Concepts

Introduction to the Validation Controls