Handling focus events for a particular object : Focus Event : Development Flash / Flex / ActionScript examples


Flash / Flex / ActionScript examples » Development » Focus Event »

 

Handling focus events for a particular object


 
package {
  import flash.display.*;
  import flash.events.*;
  import flash.text.*;

  public class Main extends Sprite {
    private var namefield:TextField = new TextField(  );
    private var passfield:TextField = new TextField(  );

    public function Main (  ) {
      namefield.width      = 100;
      namefield.height     = 30;
      namefield.border     = true;
      namefield.background = true;
      namefield.type = TextFieldType.INPUT;

      passfield.width      = 100;
      passfield.height     = 30;
      passfield.y          = 50;
      passfield.border     = true;
      passfield.background = true;
      passfield.type = TextFieldType.INPUT;

      addChild(namefield);
      addChild(passfield);

      namefield.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE,
                                 focusChangeListener);
      namefield.addEventListener(FocusEvent.KEY_FOCUS_CHANGE,
                                 focusChangeListener);
    }

    private function focusChangeListener (e:FocusEvent):void {
      if (e.target == namefield && namefield.text.length < 3) {
        trace("Name entered is less than three characters long");
        e.preventDefault(  );
      }
    }
  }
}

        



Leave a Comment / Note


 
Verification is used to prevent unwanted posts (spam). .


Flash / Flex / ActionScript examples

 Navioo Development
» Focus Event