Restricting the number of characters a user can type into a TextInput component in Flash using ActionScript 3.0

by Peter deHaan on December 22, 2008

in TextInput

The following example shows how you can control the number of characters a user can type into a Flash ActionScript 3.0 TextInput control by setting the maxChars property.

Full code after the jump.

// ActionScript 3.0
/* Requires:
 * - TextInput component in Flash library
 */
import fl.controls.TextInput;
 
var textInput:TextInput = new TextInput();
textInput.maxChars = 5;
textInput.move(10, 10);
addChild(textInput);

{ 2 comments… read them below or add one }

1 Daniel Rodriguez 12.22.08 at 12:03 pm

Hi Peter,
There is a way to restrict the max number of characters in a dynamic text field?
I’m loading a text from a data base, but in some cases the text is larger than the Dynamic Text Field and this made the text field scrollable and I don’t want that, I could make the Text Field not selectable, but I want it to be selectable.
May be is property I’m missing… an scrollable property or something
Thanks!

2 Peter deHaan 12.22.08 at 1:48 pm

Daniel Rodriguez,

As far as I know there isn’t a way to make text selectable, but not cause scrolling in a text field. The maxChars property only applies when a user is typing content in, it doesn’t apply when you set the text using ActionScript.
You could try trimming the content to a certain number of characters by using the substr() or substring() methods:

ti.text = "The quick brown fox jumps over the lazy dog.";
trace("substring():", ti.text.substring(0, ti.maxChars));
trace("substr():", ti.text.substr(0, ti.maxChars));

Peter

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: