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);
Tagged with:
 

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

  1. Daniel Rodriguez says:

    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 says:

    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 Reply

Your email address will not be published.

You may 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="" highlight="">

Spam Protection by WP-SpamFree