Recipe 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.

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 input component instance, you can set the instance's max-Chars parameter in the Component Inspector panel. A value of null or 0 allows the user to enter an unlimited number of characters. A nonzero numeric value allows the user to enter the corresponding number of characters.

You can also restrict the allowed characters that a user can enter into an input text field.




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net