How to use RegularExpressionValidator control in ASP.NET 4.0

The RegularExpressionValidator is one of ASP.NET’s most powerful validation controls. It validates text by determining whether it matches a specific pattern. All regular expressions consist of two kinds of characters:

– Literals – represent a specific defined character

– Metacharacters –  characters for special use.

When you are going to use RegularExpressionValidator you should develop regular expression as combination of literals and/or metacharacters. You can develop a simple web page and use it to test regular expressions which you are planning to use in your project. The next picture presents this idea:

 

RegularExpressionValidator control example

RegularExpressionValidator control example

In the code behind the Set This Expression button assigns a new regular expression to the RegularExpressionValidator control (using whatever text you have typed). The Validate button simply triggers a postback, which causes ASP.NET to perform validation automatically. If an error message appears, validation has failed. Otherwise, it’s successful.

 

public partial class RegularExpressionTest : System.Web.UI.Page

{

protected void cmdSetExpression_Click(Object sender, EventArgs e)

{

TestValidator.ValidationExpression = txtExpression.Text;

lblExpression.Text = “Current Expression: “;

lblExpression.Text += txtExpression.Text;

}

}

The next table describes the metacharacters for matching single characters

Character escapesDescription
Ordinary charactersCharacters other than .$^{[(|)*+?\ match themselves.
\bMatches a backspace.
\tMatches a tab.
\rMatches a carriage return.
\vMatches a vertical tab.
\fMatches a form feed.
\nMatches a newline.
\If followed by a special character (one of .$^{[(|)*+?\), this character escape matches that character literal. For example, \+ matches the + character.

 

The next table describes the metacharacters for matching types of characters

CharacterDescription
( )Groups a subexpression that will be treated as a single element. For example, (23)+ matches 23 and 232323.
|Either of two matches. For example, 3|8 matches 3 or 8.
[ ]Matches one character in a range of valid characters. For example, [C-E] matches C, D, or E.
[^ ]Matches a character that isn’t in the given range. For example, [^C-D] matches anycharacter except C and D.
.Any character except newline. For example, .here matches where and there.
\sAny whitespace character (such as a tab or space).
\SAny nonwhitespace character.
\dAny digit character.
\DAny character that isn’t a digit.
\wAny “word” character (letter, number, or underscore).
\WAny character that isn’t a “word” character (letter, number, or underscore).

 

The next table shows quantifiers. The quantifiers are placed just after a character or a range of characters and allow you to specify how many times the preceding character must be matched.

 

QuantifierDescription
*Zero or more matches of the previous character or subexpression. For example, 2*3 matches 2223 or just 3.
+One or more matches of the previous character or subexpression. For example, 2+3 matches 2223 but not 3.
?Zero or one matches
{N}N matches
{N,}N or more matches
{m,n}The previous character (or subexpression) can occur from m to n times. For example, B{1,3} matches B, BB, or BBB.

 

The next table shows a few useful regular expressions:

ContentRegular Expression Description
E-mail address\S+@\S+\.\S+Check for an at (@) sign and dot (.) and allow nonwhitespace characters only.
Password\w+Any sequence of one or more word characters (letter, space, or underscore).
Specific-length 

password

\w{4,10}A password that must be at least four characters long but no longer than ten characters.
Advanced 

password

[a-zA-Z]\w{3,9}This regular expression will allow four to ten total characters. The twist is that the first character must fall in the range of a–z or A–Z (that is to say, it must start with a nonaccented ordinary letter).
Another advanced 

password

[a-zA-Z]\w*\d+\wThis password starts with a letter character, followed by zero or more word characters, one or more digits, and then zero or more word characters. In short, it forces a password to contain one or more numbers somewhere inside it. You could use a similar pattern to require two numbers or any other special character.
Limited-length 

field

\S{4,10}This expression allows four to ten characters, but it allows special characters (asterisks, ampersands, and so on).
U.S. Social Security 

number

\d{3}-\d{2}-\d{4}A sequence of three, two, then four digits, with each group separated by a dash. You could use a similar pattern when requiring a phone number.