Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to format input text like SSN or telephone ? 1

Status
Not open for further replies.

hovercraft

Technical User
Jun 19, 2006
236
US
I've been searching the internet and help docs with CS3 and can not find any solution.

How can I have an input text field that accepts text in a given format such as social security number or U.S. based telephone number. Such that the field already displays the dashed or slahes or parenthes and when the user types it fills in the input box correctly?

example: the input box for a U.S. based telephone number would have "( ) - "
as the user types, it would fill in "(123)456-7890" without the user having to type the "()" or "-".

I beleive I maybe able to use "restrict" but I'm not sure.

Any thoughts?

Thanks in advance,
Hovercraft
 
You can do this but you need to write a script. If you need an example let me know.

How I'd approach is different though. I'd have "(", ")", and "-" as static text and have 3 input fields in between. This is more user friendly, and a lot easier to do as well :)

Kenneth Kawamoto
 
K.K. Thank You!

It just takes another way of looking at things. Here I was focusing on the input as a whole rather than breaking it down into individual boxes.

I would still be interested in seeing an example of how to accomplish this through code. All knowledge is learned and therefore good.

Can you point me in the right direction?

Thanks,
Hovercraft
 
OK, here's an example. This is purely academic - I would not suggest to take this route, as I said previously. This example is for the first part of your telephone number only, i.e. "(nnn)". I botched this up so the script is not tidy, but you can see if you want to implement this to the whole telephone number format you have to write a lot of code :)
Code:
// AS3 Document Class
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	public class Main extends Sprite {
		
		public function Main():void {	
			var tf:TextField = new TextField();
			with(tf){
				type = TextFieldType.INPUT;
				border = true;
				restrict = "0-9";
				maxChars = 4;
				x = 10;
				y = 10;
				width = 50;
				height = 20;
			}
			tf.addEventListener(Event.CHANGE, onTextChange);
			addChild(tf);
		}
		
		private function onTextChange(e:Event):void {
			var tf:TextField = e.target as TextField;
			var txt:String = tf.text;
			var len:uint = txt.length;
			if(len == 1){
				if(txt == "("){
					tf.text = "";
				}
			}
			if(len > 0){
				if(txt.charAt(0) != "("){
					tf.text = "(" + txt;
					tf.setSelection(tf.caretIndex + 1, tf.caretIndex + 1);
				}
			}
			if(len == 4){
				if(txt.charAt(len - 1) != ")"){
					tf.appendText(")");
				}
			}
			if(len < 5){
				if(txt.charAt(len - 1) == ")"){
					tf.text = txt.substring(0, len - 1);
				}
			} 
		}
	}
}

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top