TextInput.restrict

Availability

Flash Player 6 (6.0.79.0).

Edition

Flash MX 2004.

Usage

textInputInstance.restrict

Description

Property; indicates the set of characters that a user can enter in the text field. The default value is undefined. If this property is null or an empty string (""), a user can enter any character. If this property is a string of characters, the user can enter only characters in the string; the string is scanned from left to right. You can specify a range by using a dash (-).

If the string begins with ^, all characters that follow the ^ are considered unacceptable characters. If the string does not begin with ^, the characters in the string are considered acceptable. The ^ can also be used as a toggle between acceptable and unacceptable characters.

For example, the following code allows A-Z except X and Q:

Ta.restrict = "A-Z^XQ";

You can use the backslash (\) to enter a hyphen (-), caret (^), or backslash (\) character, as shown here:

\^
\-
\\

When you enter the \ character in the Actions panel within double quotation marks, it has a special meaning for the Actions panel's double-quote interpreter. It signifies that the character following the \ should be treated as is. For example, you could use the following code to enter a single quotation mark:

var leftQuote = "\'";

The Actions panel's restrict interpreter also uses \ as an escape character. Therefore, you may think that the following should work:

myText.restrict = "0-9\-\^\\";

However, since this expression is surrounded by double quotation marks, the following value is sent to the restrict interpreter: 0-9-^\, and the restrict interpreter doesn't understand this value.

Because you must enter this expression within double quotation marks, you must not only provide the expression for the restrict interpreter, but you must also escape the Actions panel's built-in interpreter for double quotation marks. To send the value 0-9\-\^\\ to the restrict interpreter, you must enter the following code:

myText.restrict = "0-9\\-\\^\\\\";

The restrict property restricts only user interaction; a script may put any text into the text field. This property does not synchronize with the Embed Font Outlines check boxes in the Property inspector.

Example

The following example provides three different uses of the restrict property. The first usage restricts input to uppercase characters A through Z, spaces, and numbers. The second usage allows any characters except the lowercase characters a through z. The third usage allows only numbers, -, ^, and \.

You must first drag a TextInput component to the Stage and give it an instance name of my_ti; then add the code to Frame 1, using only one of the following restrict statements at a time.

/**
 Requires:
  - TextInput instance on Stage (instance name: my_ti)
*/

var my_ti:mx.controls.TextInput;

// Example 1: Allow only uppercase A-Z, spaces, and digits 0-9.
my_ti.restrict = "A-Z 0-9";

// Example 2: Allow everything EXCEPT lowercase a-z.
my_ti.restrict = "^a-z"; 

// Example 3: Allow only digits 0-9, dash (-), ^, and \
my_ti.restrict = "0-9\\-\\^\\\\";