greyimg

iPhone-Like Password Masking Class in ActionScript 3.0

Posted by admin in July 28th, 2010
Published in Action Script, Action Script 3.0, Flex, Tutorial

About a week ago I got request to make password field not appearing asterisks (*) but a dot (•).  After trying to goggling to find something similar, then I got a good example by Carlos Yanez at ActiveTuts.  But still some bugs with his class, where problem on fast typing, using home, left, right arrow, delete only deleting 1 char event we did select some chars.

Someone in comment section also try to fixed with better class, but still the same problem with home, or deleting chars on selection.

With started from that article, I try to fixed and add some update and work for me.
Here the class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.janumedia.util
{
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TextEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
 
	public class PasswordMaker
	{
		private var _target:TextField;
		private var _regExp:RegExp = /./g;
		private var _timer:Timer
		private var _oriPass:String = "";
 
		public function PasswordMaker()
		{
		}
 
		public function setPassword (t:TextField, delay:Number=500) : void
		{
			_timer = new Timer(delay);
			_target = t;
			_oriPass = _target.text;
 
			_timer.addEventListener (TimerEvent.TIMER, replaceOnTime, false, 0, true);
			_target.addEventListener (TextEvent.TEXT_INPUT, startTimer, false, 0, true);
			_target.addEventListener (KeyboardEvent.KEY_DOWN, keyboardEvent, false, 0, true);
 
			// dispatchEvent in case already has text initializing
			_target.dispatchEvent( new TextEvent (TextEvent.TEXT_INPUT) );
		}
 
		public function getPassword () : String
		{
			return _oriPass;
		}
 
		private function replaceOnTime (e:TimerEvent) : void
		{
			var fm:TextFormat = _target.getTextFormat();
			fm.letterSpacing = 3;
 
			_target.text = _oriPass.replace (_regExp, "•");
			_target.setTextFormat(fm);
 
			_timer.stop();
		}
 
		private function keyboardEvent (e:KeyboardEvent) : void
		{
			// single char deletion
			if (_target.selectionBeginIndex == _target.selectionEndIndex)
			{
				if (e.keyCode == Keyboard.BACKSPACE)
				{
					_oriPass = _oriPass.substr(0, _target.caretIndex-1) + _oriPass.substr(_target.caretIndex)
 
				} else if (e.keyCode == Keyboard.DELETE)
				{
					_oriPass = _oriPass.substr(0, _target.caretIndex) + _oriPass.substr(_target.caretIndex+1)
				}
 
			// multiple chars deletion
			} else 
			{
				if (e.keyCode == Keyboard.BACKSPACE || e.keyCode == Keyboard.DELETE)
				{
					_oriPass = _oriPass.substr(0, _target.selectionBeginIndex) + _oriPass.substr(_target.selectionEndIndex)
				}
			}
		}
 
		private function startTimer (e:TextEvent) : void
		{
			if (_target.selectedText.length > 0)
			{
				_oriPass = _oriPass.substring(0,_target.selectionBeginIndex) + e.text + _oriPass.substring(_target.selectionEndIndex)
 
			} else 
			{
				_oriPass = _oriPass.substring(0,_target.caretIndex) + e.text + _oriPass.substring(_target.caretIndex)
			}
 
			if (_timer.running) _timer.reset();
 
			_timer.start();
		}
 
		public function reset () : void
		{
			_oriPass = "";
		}
 
	}
}

1 user Responded In This Post

Follow-up this post comment rss or leave a trackback

Pingback And Trackback

pingback and trackback from various blogs
mygif
July 28th, 2010 at 12:32 pm

[...] See the original post: » iPhone-Like Password Masking Class in ActionScript 3.0 JanuMedia … [...]


Add to Technorati Favorites

What I am doing

UserOnline

 

July 2010
M T W T F S S
« Jan    
 1234
567891011
12131415161718
19202122232425
262728293031  

Archives

Tags

Meta

Co-worker

Community

Personal

Profesional

Resources

Wordpress