The RegularExpressionValidator allows validating the input text by matching against a pattern of a regular expression. The regular expression is set in the ValidationExpression property.
The following table summarizes the commonly used syntax constructs for regular expressions:
Character Escapes | Description |
---|---|
\b | Matches a backspace. |
\t | Matches a tab. |
\r | Matches a carriage return. |
\v | Matches a vertical tab. |
\f | Matches a form feed. |
\n | Matches a new line. |
\ | Escape character. |
Apart from single character match, a class of characters could be specified that can be matched, called the metacharacters.
Metacharacters | Description |
---|---|
. | Matches any character except \n. |
[abcd] | Matches any character in the set. |
[^abcd] | Excludes any character in the set. |
[2-7a-mA-M] | Matches any character specified in the range. |
\w | Matches any alphanumeric character and underscore. |
\W | Matches any non-word character. |
\s | Matches whitespace characters like, space, tab, new line etc. |
\S | Matches any non-whitespace character. |
\d | Matches any decimal character. |
\D | Matches any non-decimal character. |
Quantifiers could be added to specify number of times a character could appear.
Quantifier | Description |
---|---|
* | Zero or more matches. |
+ | One or more matches. |
? | Zero or one matches. |
{N} | N matches. |
{N,} | N or more matches. |
{N,M} | Between N and M matches. |
The syntax of the control is as given:
<asp:RegularExpressionValidator ID="string" runat="server" ErrorMessage="string"
ValidationExpression="string" ValidationGroup="string">
</asp:RegularExpressionValidator>