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. 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 panel 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 panel, 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 noteTip

    When you select the ValidationExpression property in the Tag Properties panel, you can click the ellipsis button Cc295107.0b8d1f68-5b69-4cd1-b928-5f7bc2a6c4dc(en-us,Expression.40).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 Cc295107.xtlink_newWindow(en-us,Expression.40).png in the MSDN library.

  5. 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 Cc295107.xtlink_newWindow(en-us,Expression.40).png 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 Cc295107.xtlink_newWindow(en-us,Expression.40).png in the MSDN library.

See also

Send feedback about this topic to Microsoft. © 2011 Microsoft Corporation. All rights reserved.

Community Additions