ProblemYou want to perform a task when the content of a text field is modified by user input. SolutionListen for the textInput event. DiscussionYou can specify actions to be performed each time the content of a text field is changed by user input, whether that change is deleting or cutting characters, typing in a character, or pasting characters. When a user makes any change to the value of an input text field, the text field dispatches a textInput event. You can register a listener to listen for the textInput event using the flash.events.TextEvent.TEXT_INPUT constant: field.addEventListener(TextEvent.TEXT_INPUT, onTextInput); The textInput event is a TextEvent object, and it is cancelable. The TextEvent class defines a text property, which contains the value of the text entered by the user. The following example ensures that the first character the user types in a text field is not an "a": package { import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldType; import flash.events.TextEvent; import flash.events.TextEvent; public class Text extends Sprite { private var _field:TextField; public function Text( ) { _field = new TextField( ); _field.border = true; _field.background = true; _field.type = TextFieldType.INPUT; addChild(_field); _field.addEventListener(TextEvent.TEXT_INPUT, onTextInput); } private function onTextInput(event:TextEvent):void { if(event.text == "a" && _field.length == 0) { event.preventDefault( ); } } } } See AlsoRecipe 9.12 |