ASP.NET RegularExpressionValidatorControl


Validation Server ControlsValidation Server Control

Definition and Usage

The RegularExpressionValidator control is used to validate whether the input value matches a specified pattern.

Note:Both server-side and client-side validation are performed, unless the browser does not support client-side validation or the EnableClientScript property is set to false.

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


Properties

Properties Description
BackColor The background color of the RegularExpressionValidator control.
ControlToValidate The id of the control to be validated.
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, meaning the space used for displaying the message is pre-allocated.
  • Dynamic - If validation fails, the control displays the error message. If the input passes validation, no space for displaying the message is reserved on the page, meaning the space used for displaying the message is added dynamically.
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 is also displayed in the validation control.
ForeColor The foreground color of the control.
id The unique id of the control.
IsValid A Boolean value indicating whether the control specified by ControlToValidate passes validation.
runat Specifies that this control is a server control. Must be set to "server".
BackColor The background color of the RegularExpressionValidator control.
Text The message displayed when validation fails.
ValidationExpression Specifies the regular expression used to validate the input control. The syntax of the expression is different on the client and the server. JScript is used on the client. On the server, it is used according to the language you specify.


Example

RegularExpressionValidator
In this example, we declare a TextBox control, a Button control, a Label control, and a RegularExpressionValidator control in an .aspx file. The submit() function checks whether the page is valid. If it is valid, it returns "The page is valid!" in the Label control. If it is not valid, it returns "The page is not valid!" in the Label control. If validation fails, the text "The zip code must be 5 numeric digits!" will be displayed in the RegularExpressionValidator control.


Validation Server ControlsValidation Server Control Other Extensions