RegularExpressionValidator control

The RegularExpressionValidator control checks that the entry matches a pattern defined by a regular expression. This type of validation enables you to check for predictable sequences of characters, such as those in e-mail addresses, telephone numbers, postal codes, and so on.

A RegularExpressionValidator works in conjunction with another control, for example a TextBox control. Add the control you want to validate to your page before you add the RegularExpressionValidator so that you can easily associate the RegularExpressionValidator with it.

To add a RegularExpressionValidator control to a page

  1. Drag the RegularExpressionValidator control from the Toolbox task pane to your page.

  2. Select the RegularExpressionValidator in Design view, right-click it, and click Properties on the shortcut menu.

  3. In the Tag Properties task pane, select the control you want to validate with the RegularExpressionValidator in the ControlToValidate property dropdown.

  4. Set the ValidationExpression property to the regular expression that the text in the control must match to be valid.

Tip

When you select the ValidationExpression property in the Tag Properties task pane, you can click the elipsis button Cc295107.0b8d1f68-5b69-4cd1-b928-5f7bc2a6c4dc(en-us,Expression.10).gif to open the Regular Expression Editor dialog box. In this dialog box, you can choose from several predefined regular expressions, for example, Internet URL or Internet email address. You can also write your own regular expression. For more information, see Regular Expression Syntax in the MSDN library.

  1. If you are going to use a ValidationSummary control to consolidate validation error messages, set the ValidationGroup property to the name of the group of validation controls. This can be any string, as long as all the controls in the group have the same value.

For a full description of all RegularExpressionValidator control properties, see RegularExpressionValidator Members in the MSDN library.

For more information about using validation controls to validate user input, see the topics available at How-to Topics — Validation Controls in the MSDN library.

See also

Concepts

ASP.NET validation controls overview

CompareValidator control

CustomValidator control

RequiredFieldValidator control

ValidationSummary control