In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: Chapter 17 Example 9

Get Adobe Flash player
by pershiuan 16 Dec 2010
    Embed
/**
 * Copyright pershiuan ( http://wonderfl.net/user/pershiuan )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lKd1
 */

// forked from actionscriptbible's Chapter 17 Example 9
package {
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.FocusEvent;
  import flash.text.*;
  import flash.utils.Dictionary;

  public class ch17ex9 extends Sprite {
    protected const INFMT:TextFormat = new TextFormat("_sans", 12, 0, true);
    protected const OUTFMT:TextFormat = new TextFormat("_sans", 12, 0x808080, false);
    protected var prompts:Dictionary;
    protected var nameTF:TextField;
    protected var phoneTF:TextField;
    protected var emailTF:TextField;
    protected var okButton:Sprite;

    public function ch17ex9() {
      prompts = new Dictionary();
      
      nameTF = makeInputTextField();
      nameTF.text = "Your name";
      nameTF.setTextFormat(OUTFMT);
      nameTF.maxChars = 40;
      prompts[nameTF] = nameTF.text;
      
     /* phoneTF = makeInputTextField();
      phoneTF.text = "Your phone number exp:123-123-1234";
      phoneTF.setTextFormat(OUTFMT);
      phoneTF.maxChars = 12;
      phoneTF.restrict = "0-9\\-";
      prompts[phoneTF] = phoneTF.text;
      
      emailTF = makeInputTextField();
      emailTF.text = "Your email address";
      emailTF.setTextFormat(OUTFMT);
      emailTF.maxChars = 40;
      emailTF.restrict = "a-zA-Z0-9_\\-+=~!@#$%\\^.";
      prompts[emailTF] = emailTF.text;*/
      
      okButton = makeOkButton();
      validate();
    }
    
    protected function onFocusIn(event:FocusEvent):void {
      var tf:TextField = TextField(event.target);
      if (tf.text == prompts[tf]) {
        tf.text = "";
      }
      tf.setTextFormat(INFMT);
      tf.defaultTextFormat = INFMT;
    }
    
    protected function onFocusOut(event:FocusEvent):void {
      var tf:TextField = TextField(event.target);
      if (tf.text == "") {
        tf.text = prompts[tf];
        tf.setTextFormat(OUTFMT);
      }
    }
    
    protected function onChange(event:Event):void {
      validate();
    }
    
    protected function validate():void {
      if (isValid) {
        okButton.alpha = 1;
        okButton.mouseEnabled = true;
      } else {
        okButton.alpha = 0.2;
        okButton.mouseEnabled = false;
      }
    }
    
    protected function get isValid():Boolean {
      if (isEmpty(nameTF)) return false;
     /* if (isEmpty(phoneTF)
        || phoneTF.text.match(/^\d{3}-\d{3}-\d{4}$/) == null) return false;
      if (isEmpty(emailTF)
        || emailTF.text.match(/^\w+@\w+\.[a-z]{2,6}$/) == null) return false;
      //warning, this regex is not industry grade!*/
      return true;
    }
    
    protected function isEmpty(tf:TextField):Boolean {
      return (tf.text.replace(/\s*/, "").length == 0
        || tf.text == prompts[tf]);
    }
    
    protected function makeInputTextField():TextField {
      var tf:TextField = new TextField();
      tf.type = TextFieldType.INPUT;
      tf.border = true;
      tf.width = 300;
      tf.height = 18;
      tf.y = numChildren * 26;
      tf.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
      tf.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
      tf.addEventListener(Event.CHANGE, onChange);
      addChild(tf);
      return tf;
    }
    
    protected function makeOkButton():Sprite {
      var w:Number = 100, h:Number = 20;
      var btn:Sprite = new Sprite();
      var label:TextField = new TextField();
      label.width = w;
      label.height = h;
      label.defaultTextFormat = new TextFormat("_typewriter", 14, 0, true,
        null, null, null, null, TextFormatAlign.CENTER);
      label.text = "Submit";
      btn.buttonMode = true;
      btn.mouseChildren = false;
      btn.addChild(label);
      btn.graphics.lineStyle(1, 0x202030);
      btn.graphics.beginFill(0xb0b0b0, 1);
      btn.graphics.drawRect(0, 0, w, h);
      btn.graphics.endFill();
      btn.y = this.height + 20;
      addChild(btn);
      return btn;
    }
  }
}