ASP.NET CustomValidator Control

Definition and Usage

The CustomValidator control can perform user-defined validation on input controls.

Property

Property Description
BackColor Background color of the CustomValidator control.
ClientValidationFunction

Specifies the name of the custom client script function used for validation.

Note: The script must be written in a language supported by the browser, such as VBScript or JScript

If using VBScript, the function must be located within the form:

Sub FunctionName (source, arguments)

If using JScript, the function must be located within the form:

Function FunctionName (source, arguments)

ControlToValidate The id of the input control to be validated.
Display

The display behavior of error information in the validation control.

Valid values include:

  • None - Validation messages are never displayed inline.
  • Static - Space allocated for displaying validation messages in the page layout.
  • Dynamic - A space for displaying validation messages is dynamically added to the page if validation fails.
EnableClientScript Boolean value indicating whether client-side validation is enabled.
Enabled Boolean value indicating whether the validation control is enabled.
ErrorMessage

Text of the error information displayed in the ValidationSummary control when validation fails.

Note: If the ErrorMessage property is set but the Text property is not set, the ErrorMessage property value will also be displayed in the validation control.

ForeColor The foreground color of the control.
id The unique id of the control.
IsValid Boolean value indicating whether the associated input control passes validation.
OnServerValidate Specifies the name of the server-side validation script function to be executed.
runat Specifies that the control is a server control. It must be set to "server".
Text Text displayed when validation fails.

Instance

CustomValidator
In this example, we declared two Label controls, one TextBox control, one Button control, and one CustomValidator control in the .aspx file. The user() function can detect the length of the input value. If the length is less than 8 or greater than 16, the text "The username must be between 8 and 16 characters!" will be displayed in the CustomValidator control.