ASP.NET RegularExpressionValidator Control

This validator is used to validate the value of an input control against the pattern defined by a regular expression.

It allows us to check and validate predictable sequences of characters like: e-mail address, telephone number etc.

The ValidationExpression property is used to specify the regular expression, this expression is used to validate input control.

RegularExpression Properties

PropertyDescription
AccessKeyIt is used to set keyboard shortcut for the control.
BackColorIt is used to set background color of the control.
BorderColorIt is used to set border color of the control.
FontIt is used to set font for the control text.
ForeColorIt is used to set color of the control text.
TextIt is used to set text to be shown for the control.
ToolTipIt displays the text when mouse is over the control.
VisibleTo set visibility of control on the form.
HeightIt is used to set height of the control.
WidthIt is used to set width of the control.
ErrorMessageIt is used to set error message that display when validation fails.
ControlToValidateIt takes ID of control to validate.
ValidationExpressionIt is used to set regular expression to determine validity.

Example

Here, in the following example, we are explaining how to use RegularExpressionValidator control to validate the user input against the given pattern.

// RegularExpressionDemo.aspx

Output:

It produces the following output when view in the browser.

ASP Regular 1

It will validate email format as we specified in regular expression. If validation fails, it throws an error message.

ASP Regular 2