Flex AS3 Password Creator / Generator


Ever wanted to create or generate passwords within Adobe Flex / Flash Builder or AS3 ?

Well it’s quite simple to do this.

You only need 11 lines of code for generating a password (well 13 if you count the function statement)

With this piece of code you can create any kind of password for your user.

public function createpw(strHash:String = 'abchefghjkmnpqrstuvwxyz0123456789',lnHash:Number = 5):String
{
	var i:Number = 0;
	var hash:String = "";
	var nLenght:Number = strHash.length;
	while (i <= lnHash)
	{
		var num:Number = Math.round(Math.random()*nLenght);
		hash += strHash.charAt(num);
		i++;
	}
	return hash;
}

createpw () function call examples:


If you want createpw() to create a 5 letter password

txtField.text = createpw();


if you want createpw() to create a 4 digit password

txtField.text = createpw(‘1234567890’, 4);


if you want createpw() to generate 12 any character password

txtField.text = createpw('acbdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()', 12);


if you want createpw() to create a 8 any character password with upper and lower case characters

txtField.text = createpw('acbdefghijklmnopqrstuvwxyzACBDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()', 8);

Theo van der Sluijs

Theo van der Sluijs (1975) is Software Development Manager at Bax-shop.nl. You can read more in dutch on his “Dutch Blog“, he also created Gebruikmaar.nl. He is a member of the Board of the Dutch Flex Usergroup and Goesweb.net.

Google+ Twitter LinkedIn 



VN:F [1.9.13_1145]
Rating: 10.0/10 (6 votes cast)
VN:F [1.9.13_1145]
Rating: +1 (from 3 votes)
Flex AS3 Password Creator / Generator, 10.0 out of 10 based on 6 ratings

Related Posts

3 comments

  1. Merso Maniac

    To avoid an empty string one must change

    var num:Number = Math.round(Math.random()*nLenght);

    to

    var num:Number = Math.floor(Math.random()*nLenght);

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: +2 (from 2 votes)
  2. var num:Number = Math.round(Math.random()*(nLenght – 1));
    to get the exact amount of chars
    cheers =)

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  3. flashflex

    Thanks Nico it worked..

    adobe flash,actionscript, as3, html5, flex

    -sara http://www.designscripting.com

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)

Leave a Reply

Get Adobe Flash playerPlugin by wpburn.com wordpress themes