Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Build Your Own ASP.NET 2.0 Web Site Using CSharp And VB (2006) [eng]-1.pdf
Скачиваний:
143
Добавлен:
16.08.2013
Размер:
15.69 Mб
Скачать

Chapter 6: Using the Validation Controls

This control isn’t 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 Figure 6.8. The server-side list will still be shown to users who don’t have JavaScriptenabled 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. They’re 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

RegularExpressionValidator

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.

Let’s 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 that’s 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

Chapter 6: Using the Validation Controls

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 expression 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

Description

Regular Expression

email address

^\S+@\S+\.\S+$

web URL

^https?://\S+\.\S+$

US phone numbers ((555) 555-5555 or

^\(?\d{3}\)?(\s|-)\d{3}-

555-555-5555)

\d{4}$

international phone numbers (begins with a

^\d(\d|-){7,20}$

digit, followed by between seven and 20 digits

 

and/or dashes)

 

five-digit ZIP code

^\d{5}$

nine-digit ZIP code

^\d{5}-\d{4}$

either five-digit or nine-digit ZIP code

^(\d{5})|(\d{5}\-\d{4})$

US social security number

^\d{3}-\d{2}-\d{4}$

By referencing the components of these regular expressions in Table 6.2, you should begin to see how they work. If you’d like more information on regular expressions, try the following resources:

Regular Expression Library2

a searchable library of regular expressions

Using Regular Expressions in PHP3

a great article on the use of regular expressions and PHP

1Jeffrey E. F. Friedl, Mastering Regular Expressions, Third Edition (Sebastopol: O’Reilly Media), 2006.

2http://www.regexlib.com/

3http://www.sitepoint.com/article/regular-expressions-php

238