8.5. Limiting the Length of User Input

Problem

You want to restrict the number of characters that a user can enter into an input text field or text input component instance.

Solution

With the text field selected, enter a nonzero numeric value in the Maximum Characters field of the Property inspector. Optionally, set the instance’s maxChars property to a nonzero numeric value using ActionScript.

Or, if you are using a text input component, set the maxChars parameter to a nonzero numeric value.

Discussion

In some scenarios, you want to restrict the number of characters that a user can type into an input field. For example, you may want to limit a user to five characters when entering a U.S. zip code. Flash lets you limit the length of the input for both input text fields and text input components.

For an input text field, you can use the Maximum Characters option in the Property inspector (see Figure 8-2). Enter a nonzero numeric value while you have selected the input text field instance on the stage. A value of 0 allows the user to enter an unlimited number of characters.

Use the Maximum Characters field to limit the number of characters that a user can enter into an input text field

Figure 8-2. Use the Maximum Characters field to limit the number of characters that a user can enter into an input text field

Optionally, you can use ActionScript to set the maximum number of characters by assigning a value to the instance’s maxChars property. For example:

	tInputField.maxChars = 10;

If you’re using a text ...

Get Flash 8 Cookbook 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.