30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 20 ASP .<strong>NET</strong>, Web Forms and Web Controls 971<br />

51 <br />

54 <br />

55<br />

56 <br />

59 <br />

60<br />

61 <br />

62 <br />

63 <br />

Fig. 20.21 Valida<strong>to</strong>rs used in a Web Form that generates possible letter combinations<br />

from a phone number (part 2 of 2).<br />

<strong>to</strong> match another Web control’s content against a regular expression. (The use of regular<br />

expressions is introduced in Chapter 15, Strings, Characters and Regular Expressions.)<br />

Lines 35–41 create a RegularExpressionValida<strong>to</strong>r named phoneNumber-<br />

Valida<strong>to</strong>r. Property ErrorMessage’s text (lines 37–38) is displayed on the Web Form<br />

if the validation fails. The regular expression that validates the input is assigned <strong>to</strong> property<br />

ValidationExpression in line 38. The input is valid if it matches the regular expression<br />

^\d{3}-\d{4}$ (i.e., if exactly 3 digits are followed by a hyphen and exactly 4<br />

digits, where the 3 digits are at the beginning of the string and the 4 digits are at the end of<br />

the string).<br />

The clicking of property ValidationExpression in the Properties window<br />

displays a dialog that contains a list of common regular expressions for phone numbers, ZIP<br />

codes and other formatted information. <strong>How</strong>ever, we write our own regular expression in<br />

this example, because the phone number input should not contain an area code. Line 39<br />

associates phoneTextBox with phoneNumberValida<strong>to</strong>r by setting property<br />

ControlToValidate <strong>to</strong> phoneTextBox. This indicates that phoneNumber-<br />

Valida<strong>to</strong>r verifies the phoneTextBox’s contents. If the user inputs text that does not<br />

have the correct format and attempts <strong>to</strong> submit the form, the ErrorMessage text is displayed<br />

in red.<br />

This example also uses a RequiredFieldValida<strong>to</strong>r <strong>to</strong> ensure that the text box<br />

is not empty when the HTML form is submitted. Lines 43–49 define RequiredField-<br />

Valida<strong>to</strong>r phoneInputValida<strong>to</strong>r, which confirms that phoneTextBox’s content<br />

is not empty. If the user does not input any data in phoneTextBox and attempts <strong>to</strong><br />

submit the form, validation fails, and the ErrorMessage for this valida<strong>to</strong>r is displayed<br />

in red. If the valida<strong>to</strong>r is successful, a multiline TextBox named outputTextBox (lines<br />

29–33) displays the words generated from the phone number. Notice that the Visible<br />

property initially is set <strong>to</strong> False when the server returns its HTML response.<br />

Figure 20.22 depicts the code-behind file for the ASPX file in Fig. 20.21. Notice that<br />

this code-behind file does not contain any implementation related <strong>to</strong> the valida<strong>to</strong>rs. We say<br />

more about this soon.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!