<script type="text/javascript">
var PasswordStrength ={
Level : ["極佳","一般","較弱","太短"],//強度提示資訊
LevelValue : [15,10,5,0],//和上面提示資訊對應的強度值
Factor : [1,2,5],//強度加權數,分別為字母,數字,其它
KindFactor : [0,0,10,20],//密碼含幾種組成的權數
Regex : [/[a-zA-Z]/g,/\d/g,/[^a-zA-Z0-9]/g] //字元,數字,非字元數字(即特殊符號)
}
PasswordStrength.StrengthValue = function(pwd)
{
var strengthValue = 0;
var ComposedKind = 0;
for(var i = 0 ; i < this.Regex.length;i++)
{
var chars = pwd.match(this.Regex[i]);//匹配當前密碼中符合指定正則的字元,如果有多個字元以','分隔
if(chars != null)
{
strengthValue += chars.length * this.Factor[i];
ComposedKind ++;
}
}
strengthValue += this.KindFactor[ComposedKind];
return strengthValue;
}
PasswordStrength.StrengthLevel = function(pwd)
{
var value = this.StrengthValue(pwd);
for(var i = 0 ; i < this.LevelValue.length ; i ++)
{
if(value >= this.LevelValue[i] )
return this.Level[i];
}
}
function loadinputcontext(o)
{
var showmsg=PasswordStrength.StrengthLevel(o.value);
switch(showmsg)
{
case "太短": showmsg+=" <img src='images/level/1.gif' width='88' height='11'
/>";
break;
case "較弱": showmsg+=" <img src='images/level/2.gif' width='88' height='11'
/>";
break;
case "一般": showmsg+=" <img src='images/level/3.gif' width='88' height='11'
/>";
break;
case "極佳": showmsg+=" <img src='images/level/4.gif' width='88' height='11'
/>";
break;
}
document.getElementById('showmsg').innerHTML = showmsg;
}
</script>
<input name="password" type="password" id="password" size="20" onkeyup="return loadinputcontext(this);" />
<span id="showmsg"></span>