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);

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);