= minLength &&
StringUtil.trim(password.text) === StringUtil.trim(password2.text)) ? StringUtil.trim(password.text) : null;
}
// Toggle the mouse out/mouse over display
// Known issue: The cursor jumps when switching back and forth between modes
public function hidePassword(tbox : TextInput, hide : Boolean) : void {
if (showOnMouseOver)
tbox.displayAsPassword= hide;
}
// give parent controls a way to ask if we can trust the data. Use this before allowing actions like clicking a Submit button.
public function validate() : Boolean {
var vResult : ValidationResultEvent = passlength1validator.validate();
if (vResult.type == ValidationResultEvent.INVALID)
return false;
vResult = passlength2validator.validate();
if (vResult.type == ValidationResultEvent.INVALID)
return false;
return (getPassword() != null);
}
// Calculate the password's score and update the meter accordingly.
private function score() : void {
var score : Number = scorePassword(password.text);
meter.setProgress(Math.min(score, 40), 40);
meter.setStyle("barColor", getColor(score));
meter.label = getDescriptor(score);
}
// Show the error if password1 and password2 don't match
private function match() : void {
errorLine.visible = (
StringUtil.trim(password.text).length > 0 &&
StringUtil.trim(password2.text).length > 0 &&
(StringUtil.trim(password.text) === StringUtil.trim(password2.text)) == false);
}
// Shortcut to determine if the string is an uppercase character for counting
public static function isUpperCase(char : Number) : Boolean {
return (char >= 65 && char <= 90);
}
// Shortcut to determine if the string is a lowercase character for counting
public static function isLowerCase(char : Number) : Boolean {
return (char >= 97 && char <= 122);
}
// Shortcut to determine if the character is a digit
public static function isDigit(char : Number) : Boolean {
return (char >= 48 && char <=57);
}
// anything but a space, a letter, a number, or a key like control or escape is considered a special character
public static function isSpecial(char : Number) : Boolean {
return ( char >= 33 && char != 127 && !isUpperCase(char) && !isLowerCase(char) && !isDigit(char) );
}
// get the score based on the password length
public static function scoreLength(length : Number) : Number {
if (length == 0) return 0;
if (length <= 4) return 3;
if (length <= 7) return 6;
if (length <= 10) return 9;
if (length <= 15) return 12;
return 15;
}
// return the scoring of the password
public static function scorePassword(pass : String) : Number {
var score : Number = 0;
// score for length
var pass : String = pass;
var length : Number = pass.length;
score += scoreLength(length);
// we'll count how many of these we see
var lcase : Number = 0;
var ucase : Number = 0;
var numbers : Number = 0;
var special : Number = 0;
// count characters of type for future use
for (var i : int = 0; i < pass.length; i++) {
var char : Number = pass.charCodeAt(i);
if (isLowerCase(char))
lcase++;
if (isUpperCase(char))
ucase++;
if (isSpecial(char))
special++;
if (isDigit(char))
numbers++;
}
// at least 1 lowercase
if (lcase >= 1)
score += 1;
// at least 1 capital
if (ucase >= 1)
score += 5;
// at least 3 capital
if (ucase >= 3)
score += 2;
// at least 1 number
if (numbers >= 1)
score += 5;
// at least 3 #s
if (numbers >= 3)
score += 5;
// at least 1 special
if (special >= 1)
score += 5;
// at least 2 special
if (special >= 2)
score += 5;
// combo : number and special
if (numbers >= 1 && special >= 1)
score += 1;
// combo : letter and special
var letters : Number = ucase + lcase;
if (letters >= 1 && special >= 1)
score += 1;
// combo: Letter and number
if (letters >= 1 && numbers >= 1)
score += 2;
// combo: upper and lower case
if (ucase >= 1 && lcase >= 1)
score += 2;
return score;
}
// returns the label for the bar based on the score
public static function getDescriptor (score : Number) : String {
if (score < 10) return ("Very Weak");
if (score < 20) return ("Weak")
if (score < 30) return ("Medium")
if (score < 40) return ("Strong")
return ("Very Strong")
}
// returns the color of the bar based on the score
public static function getColor (score : Number) : Number {
if (score < 10) return 0xFF0000;
if (score < 20) return 0xFF3300;
if (score < 30) return 0xFFCC00;
if (score < 40) return 0x33CC33;
return 0x00FF00;
}
]]>