ASP.NET RegularExpressionValidator control

Definition and Usage

RegularExpressionValidator control is used to validate whether the input value matches the pattern specified by the regular expression.

Note:The server-side and client-side validation will be executed simultaneously unless the browser does not support client-side validation or EnableClientScript is set to false.

Note:If the input control is empty, the validation will fail. Please use RequiredFieldValidator control to make the field required.

Property

Property Description
BackColor Background color of RangeValidator control
ControlToValidate The id of the control to be validated
Display

The display behavior of the validation control.

Valid values include:

  • None - Validation messages are never displayed inline.
  • Static - A space for displaying validation messages is allocated in the page layout.
  • Dynamic - If validation fails, a space for displaying validation messages will be dynamically added to the page.
EnableClientScript Boolean value specifying whether client-side validation is enabled.
Enabled Boolean value specifying 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 Boolean value indicating whether the associated input control passes validation.
runat Specifies that the control is a server control. It must be set to "server".
Text The message displayed when validation fails.
ValidationExpression Specifies the regular expression to validate the input control. The syntax of the expression is different on the client and server sides.

Example

RegularExpressionValidator
In this example, we have declared a TextBox control, a Button control, a Label control, and a RegularExpressionValidator control in the .aspx file. The submit() function checks if the page is valid. If it is valid, it returns "Page is valid!" in the Label control. If it is invalid, it returns "Page is invalid!" in the Label control. If the validation fails, the text "The postal code must be five digits!" will be displayed in the RegularExpressionValidator control.