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
RegularExpressionValidator
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:
^\S+@\S+\.\S+$
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:
books@sitepoint.com
zac@host.modulemedia.com
joe_bloggs@yahoo.co.uk
However, the regular expression would fail if the user typed in one of these entries:
236
Chapter 6: Using the Validation Controls
books@sitepoint
joe bloggs@yahoo.co.uk
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"
ControlToValidate="emailTextBox"
ErrorMessage="Email address is required!"
SetFocusOnError="True" Display="Dynamic" />
<asp:RegularExpressionValidator id="emailValidator"
runat="server" ControlToValidate="emailTextBox"
ValidationExpression="^\S+@\S+\.\S+$"
ErrorMessage="You must enter a valid email address!" />
</p>
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
237
RegularExpressionValidator
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!
1
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
^\(?\d{3}\)?(\s|-)\d{3}-
\d{4}$
US phone numbers ((555) 555-5555 or
555-555-5555)
^\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
2
a searchable library of regular expressions
Using Regular Expressions in PHP
3
a great article on the use of regular expressions and PHP
1
Jeffrey E. F. Friedl, Mastering Regular Expressions, Third Edition (Sebastopol: OReilly Media), 2006.
2
http://www.regexlib.com/
3
http://www.sitepoint.com/article/regular-expressions-php
238
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.