Javascript决定密码的强度(自写代码)。

在一些网站,比如优酷,经常会出现密码强度:
今天我做了JS密码强度判断,效果如下:
下面是代码:
复制代码代码如下所示:


JS判断密码的强度。

键入密码判断
函数的CharMode(中){
如果(在< = 48中< = 57)数字
返回1;
如果(在< = 65在< = 90)中
返回2;
如果(在< = 97在< = 122)中小写
返回4;
其他的
返回8;
}
/ / bittotal功能
计算密码模式
功能bittotal(NUM){
模式= 0;
对于(i = 0;i < 4;i + +){
如果(数字1)模式+;
1;
}
模式的回归;
}
返回强度级别
功能checkstrong(SPW){
如果(SPW。长度<= 4)
返回0;密码太短
模式= 0;
为(i = 0;i < spw.length;i++){
密码模式
模式| = CharMode(spw.charcodeat(我));
}
返回bittotal(模式);
}

显示/颜色
功能pwstrength(PWD){
o_color =# eeeeee ;
l_color =# FF0000;
m_color =# ff9900 ;
h_color =# 33cc00 ;
如果(PWD = = null | | pwd = =){
颜色= mcolor = = o_color hcolor;
}
别的{
s_level = checkstrong(PWD);
开关(s_level){
案例0:
颜色= mcolor = = o_color hcolor;
案例1:
颜色= l_color;
mcolor = = o_color hcolor;
打破;
案例2:
颜色= = m_color mcolor;
hcolor = o_color;
打破;
违约:
颜色= mcolor = = h_color hcolor;
}
}
document.getelementbyid(strength_l)。风格的背景颜色;
document.getelementbyid(strength_m)。风格。背景= mcolor;
document.getelementbyid(strength_h)。风格。背景= hcolor;
返回;
}






密码:





密码强度:
虚弱的
在里面
坚强的