原理:
采用打分的机制,打分分为3类(基础分、加分、减分),先求出基础分,在计算加分的部分,最后减去要扣除的分数就为最终总分。
规则:
密码可输入类型(字符,字母大写,字母小写,特殊字符)。
基础分为,密码长度,一个长度为一分,大于18个字符都为18分;密码里面包含一种可输入类型,基础分加4分。
加分为,一种密码可输入类型的总数量大于等于2个,加分2分,如果总数量大于等于5,加分4分。
减分为,如果有连续重复的单个种类字符,则重复一次减1分。
总分50分。
0~10分:不合格(弱)
11~20分:一般
21~30分:中
31~40分:强
41~50分:安全
*分数范围可以自由调整和搭配,其实整个打分规则都可以根据需要修改
举例:
<script>
function testpass(password,username)
{
var score = 0;
if (password.length < 4 ) { return -4; }
if (typeof(username) != "undefined" && password.toLowerCase() == username.toLowerCase()){return -2}
score += password.length * 4;
score += ( repeat(1,password).length - password.length ) * 1;
score += ( repeat(2,password).length - password.length ) * 1;
score += ( repeat(3,password).length - password.length ) * 1;
score += ( repeat(4,password).length - password.length ) * 1;
if (password.match(/(.*[0-9].*[0-9].*[0-9])/)){ score += 5;}
if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){ score += 5 ;}
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){ score += 10;}
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)){ score += 15;}
if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/)){ score += 15;}
if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/)){score += 15;}
if (password.match(/^w+$/) || password.match(/^d+$/) ){ score -= 10;}
if ( score < 0 ){score = 0;}
if ( score > 100 ){ score = 100;}
return score;
function repeat(len,str)
{
var res = "";
for (var i = 0; i < str.length; i++ )
{
var repeated = true;
for (var j = 0, max = str.length - i - len; j < len && j < max; j++)
{
repeated = repeated && (str.charAt(j + i) == str.charAt(j + i + len));
}
if (j < len) repeated = false;
if (repeated) {
i += len - 1;
repeated = false;
}
else
{
res += str.charAt(i);
}
}
return res;
}
}
function checkpass(pass)
{
var username = document.getElementById("username").value;
var score = testpass(pass.value,username);
var password_label = document.getElementById("password_label");
if(score == -4)
{
password_label.innerHTML = "太短";
}
else if(score == -2)
{
password_label.innerHTML = "与用户名相同";
}
else
{
var color = score < 34 ? "#edabab" : (score < 68 ? "#ede3ab" : "#d3edab");
var text = score < 34 ? "弱" : (score < 68 ? "一般" : "很好");
var width = score + "%";
password_label.innerHTML = ""+text+"";
}
}
</script>
请输入用户名:
请输入密码:
http://m.bbyears.com/jiaocheng/37169.html
推荐访问: