解读ASP.NET密码强度验证代码实例分享
程序员文章站
2024-02-29 17:05:46
效果如下:
输入密码:密码强度:
弱
中
强
代码如下:复制代码 代码如下:<%@ page language="c#" au...
效果如下:
输入密码:
密码强度:
密码强度:
弱 | 中 | 强 |
代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<mce:script language="javascript" type="text/javascript"><!--
//charmode函数
//测试某个字符是属于哪一类.
function charmode(in){
if (in>=48 && in <=57) //数字
return 1;
if (in>=65 && in <=90) //大写字母
return 2;
if (in>=97 && in <=122) //小写
return 4;
else
return 8; //特殊字符
}
//bittotal函数
//计算出当前密码当中一共有多少种模式
function bittotal(num){
modes=0;
for (i=0;i<4;i++){
if (num & 1) modes++;
num>>>=1;
}
return modes;
}
//checkstrong函数
//返回密码的强度级别
function checkstrong(spw){
if (spw.length<=4)
return 0; //密码太短
modes=0;
for (i=0;i<spw.length;i++){
//测试每一个字符的类别并统计一共有多少种模式.
modes|=charmode(spw.charcodeat(i));
}
return bittotal(modes);
}
//pwstrength函数
//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色
function pwstrength(pwd){
o_color="#e0f0ff";
l_color="#ff0000";
m_color="#ff9900";
h_color="#33cc00";
if (pwd==null||pwd==''){
lcolor=mcolor=hcolor=o_color;
}
else
{
s_level=checkstrong(pwd);
switch(s_level)
{
case 0:
lcolor=mcolor=hcolor=o_color;
case 1:
lcolor=l_color;
mcolor=hcolor=o_color;
break;
case 2:
lcolor=mcolor=m_color;
hcolor=o_color;
break;
default:
lcolor=mcolor=hcolor=h_color;
}
}
document.getelementbyid("strength_l").style.background=lcolor;
document.getelementbyid("strength_m").style.background=mcolor;
document.getelementbyid("strength_h").style.background=hcolor;
return;
}
// --></mce:script>
<body>
<form id="form1" runat="server">
<div>
输入密码:<asp:textbox id="textbox1" runat="server" onkeyup=pwstrength(this.value) onblur=pwstrength(this.value) ></asp:textbox><br />
密码强度:
<table border="1" cellpadding="1" bordercolordark="#fdfeff" bordercolorlight="#99ccff" cellspacing="1" style="width: 200px; display: inline; background-color:#e0f0ff">
<tr>
<td id="strength_l" style="width: 100px; height: 19px;" align="center">
弱</td>
<td id="strength_m" style="width: 100px; height: 19px;" align="center">
中</td>
<td id="strength_h" style="width: 100px; height: 19px;" align="center">
强</td>
</tr>
</table>
</div>
</form>
</body>
</html>
复制代码 代码如下:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<mce:script language="javascript" type="text/javascript"><!--
//charmode函数
//测试某个字符是属于哪一类.
function charmode(in){
if (in>=48 && in <=57) //数字
return 1;
if (in>=65 && in <=90) //大写字母
return 2;
if (in>=97 && in <=122) //小写
return 4;
else
return 8; //特殊字符
}
//bittotal函数
//计算出当前密码当中一共有多少种模式
function bittotal(num){
modes=0;
for (i=0;i<4;i++){
if (num & 1) modes++;
num>>>=1;
}
return modes;
}
//checkstrong函数
//返回密码的强度级别
function checkstrong(spw){
if (spw.length<=4)
return 0; //密码太短
modes=0;
for (i=0;i<spw.length;i++){
//测试每一个字符的类别并统计一共有多少种模式.
modes|=charmode(spw.charcodeat(i));
}
return bittotal(modes);
}
//pwstrength函数
//当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色
function pwstrength(pwd){
o_color="#e0f0ff";
l_color="#ff0000";
m_color="#ff9900";
h_color="#33cc00";
if (pwd==null||pwd==''){
lcolor=mcolor=hcolor=o_color;
}
else
{
s_level=checkstrong(pwd);
switch(s_level)
{
case 0:
lcolor=mcolor=hcolor=o_color;
case 1:
lcolor=l_color;
mcolor=hcolor=o_color;
break;
case 2:
lcolor=mcolor=m_color;
hcolor=o_color;
break;
default:
lcolor=mcolor=hcolor=h_color;
}
}
document.getelementbyid("strength_l").style.background=lcolor;
document.getelementbyid("strength_m").style.background=mcolor;
document.getelementbyid("strength_h").style.background=hcolor;
return;
}
// --></mce:script>
复制代码 代码如下:
<body>
<form id="form1" runat="server">
<div>
输入密码:<asp:textbox id="textbox1" runat="server" onkeyup=pwstrength(this.value) onblur=pwstrength(this.value) ></asp:textbox><br />
密码强度:
<table border="1" cellpadding="1" bordercolordark="#fdfeff" bordercolorlight="#99ccff" cellspacing="1" style="width: 200px; display: inline; background-color:#e0f0ff">
<tr>
<td id="strength_l" style="width: 100px; height: 19px;" align="center">
弱</td>
<td id="strength_m" style="width: 100px; height: 19px;" align="center">
中</td>
<td id="strength_h" style="width: 100px; height: 19px;" align="center">
强</td>
</tr>
</table>
</div>
</form>
</body>
</html>