15.4. Guiding Input with a Masked Text Box

As you discover in Chapter 19, it's dangerous to trust users to enter valid data. Even if their input isn't evil (although you should assume it is until proven otherwise!), whatever users type can be light years from what you intend. If you ask them not to enter dashes, expect dashes anyway.

Formatted input with a mask lets you guide users whenever they enter a date, IP address, telephone number, currency value, or credit card number. For example, Figure 15-5 shows the use of a masked text box for a North American telephone number format. The text box at the top is complete and the mask elements have disappeared. The lower text box shows a formatted number with the cursor still in the text box. The commonly used telephone number format puts the area code (such as 705) in parentheses, followed by a space, and then three digits, a dash, and the remaining four digits.

Figure 15-5. A mask for a North American telephone format.

15.4.1. Creating a masked input

Follow these steps to create the masked input used in Figure 15-5:

  1. Add an ASP.NET TextBox control to an AJAX Web form.

  2. From the TextBox control's Tasks menu, choose Add Extender.

  3. In the Choose an Extender dialog box, select the MaskedEditExtender and then click OK.

  4. In the TextBox control's Properties window (F4), in the MaskedEditExtender node (usually TextBox1_MaskedEditExtender), set ...

Get ASP.NET 3.5 For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.