This control isnt particularly good-looking, but you can see its potential. If you
set the Display properties of all the other validation controls on the page to
None, you could use a ValidationSummary to show all the errors in one place.
If you set the ShowMessageBox property of the ValidationSummary control to
True, the list of errors will be shown in a JavaScript alert box similar to Fig-
ure 6.8. The server-side list will still be shown to users who dont have JavaScript-
enabled browsers.
Figure 6.8. Showing validation errors in a dialog
The RegularExpressionValidator lets you specify a regular expression that
describes all the allowable values for a field. Regular expressions are powerful
tools for manipulating strings, and are supported by many programming languages.
Theyre commonly used to check for patterns inside strings. Consider, for instance,
the following regular expression:
In plain English, this expression will match any string that begins with one or
more non-whitespace characters followed by the @ character, then one or more
non-whitespace characters, then a dot (.), then one or more non-whitespace
characters, followed by the end of the string.
This regular expression describes any one of these email addresses:
However, the regular expression would fail if the user typed in one of these entries:
Chapter 6: Using the Validation Controls
Although regular expressions cannot check to see if the email address itself is
valid, they can, at the very least, provide a means for us to determine whether
or not the user has entered a string of characters that has all the key components
of a valid email address.
Lets change the username field in our login form to an email address field, and
validate it using the RegularExpressionValidator control.
File: Login.aspx (excerpt)
<asp:TextBox id="emailTextBox" runat="server" />
<asp:RequiredFieldValidator id="emailReq" runat="server"
ErrorMessage="Email address is required!"
SetFocusOnError="True" Display="Dynamic" />
<asp:RegularExpressionValidator id="emailValidator"
runat="server" ControlToValidate="emailTextBox"
ErrorMessage="You must enter a valid email address!" />
The important property within this control is ValidationExpression, to which
we assign the regular expression thats appropriate to handle our custom validation
functionality. Figure 6.9 shows the error message that appears when a user enters
an incorrect email address.
Figure 6.9. Using the RegularExpressionValidator control
Some Useful Regular Expressions
Writing regular expressions can be tricky, and a comprehensive discussion is
outside the scope of this book. Many of the other regular expressions presented
here are nowhere near as rigorous as they could be, but are still quite useful. The
book Mastering Regular Expressions, by Jeffrey E. F. Friedl, contains a single expres-
sion for checking email addresses that tops 6,000 characters!
Table 6.1 outlines the usage of some simple regular expressions.
Table 6.1. Some simple regular expressions
Regular ExpressionDescription
^\S+@\S+\.\S+$email address
^https?://\S+\.\S+$web URL
US phone numbers ((555) 555-5555 or
^\d(\d|-){7,20}$international phone numbers (begins with a
digit, followed by between seven and 20 digits
and/or dashes)
^\d{5}$five-digit ZIP code
^\d{5}-\d{4}$nine-digit ZIP code
^(\d{5})|(\d{5}\-\d{4})$either five-digit or nine-digit ZIP code
^\d{3}-\d{2}-\d{4}$US social security number
By referencing the components of these regular expressions in Table 6.2, you
should begin to see how they work. If youd like more information on regular
expressions, try the following resources:
Regular Expression Library
a searchable library of regular expressions
Using Regular Expressions in PHP
a great article on the use of regular expressions and PHP
Jeffrey E. F. Friedl, Mastering Regular Expressions, Third Edition (Sebastopol: OReilly Media), 2006.
Chapter 6: Using the Validation Controls

Get Build Your Own ASP.NET 2.0 Web Site Using C# & VB, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.