Skip to content

Instantly share code, notes, and snippets.

@40
Created July 2, 2012 05:45
Show Gist options
  • Select an option

  • Save 40/3031308 to your computer and use it in GitHub Desktop.

Select an option

Save 40/3031308 to your computer and use it in GitHub Desktop.
Phone Number Format TextField Automatically (AS3)
var phoneField:TextField = new TextField();
with (phoneField) {
    type=TextFieldType.INPUT;
    maxChars=12;
    restrict="0-9";
    border=true;
    width=100;
    height=20;
    x=y=20;
}
addChild(phoneField);
 
phoneField.addEventListener(TextEvent.TEXT_INPUT, onInput);
 
function onInput(evt:TextEvent):void {
    if (phoneField.length==3 || phoneField.length==7) {
        phoneField.appendText("-");
        var leng:int=phoneField.text.length;
        phoneField.setSelection(leng, leng);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment