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






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(  );
      }
    }
  }
}

        








Related examples in the same category

1.FocusEvent.KEY_FOCUS_CHANGE
2.Handling focus events globally
3.Focus and Tab Events
4.focusIn and focusOut Events
5.FocusEvent.MOUSE_FOCUS_CHANGE