ASP.NET CustomValidatorControl


Validation Server ControlsValidation Server Control

Definition and Usage

The CustomValidator control allows you to write methods that handle validation of input values.


Properties

Properties Description
BackColor The background color of the CustomValidator control.
ClientValidationFunction Specifies the name of the client-side script function to be executed.Note:The script must be written in a language supported by the browser, such as VBScript or JScript.

When using VBScript, the function must be placed inside the form:

Sub FunctionName (source, arguments)

When using JScript, the function must be placed inside the form:

Function FunctionName (source, arguments)

ControlToValidate The id of the control to validate.
Display The display behavior of the validation control. Valid values are:
  • None - The control is not displayed. It is only used to display error messages in the ValidationSummary control.
  • Static - If validation fails, the control displays the error message. Even if the input passes validation, space for displaying the message is reserved on the page, i.e., the space used for the message is pre-allocated.
  • Dynamic - If validation fails, the control displays the error message. If the input passes validation, no space is reserved on the page for the message, i.e., the space used for the message is dynamically added.
EnableClientScript A Boolean value that specifies whether client-side validation is enabled.
Enabled A Boolean value that specifies whether the validation control is enabled.
ErrorMessage The text displayed in the ValidationSummary control when validation fails.Note:If the Text property is not set, the text will also be displayed in the validation control.
ForeColor The foreground color of the control.
id The unique id of the control.
IsValid A Boolean value that indicates whether the control specified by ControlToValidate 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. Must be set to "server".
Text The message displayed when validation fails.


Example

CustomValidator
In this example, we declare two Label controls, a TextBox control, a Button control, and a 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 "A username must be between 8 and 16 characters!" is displayed in the CustomValidator control.


Validation Server ControlsValidation Server Control Other Extensions