关于密码强度

最近百度了一下GOOGLE的密码强度的方法

实验成功了

贴上代码方便大家使用:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>

</head>
<td>密码:</td>
<td><input id=pwd οnkeyup=CreateRatePasswdReq(this); type=password value=""><span

style="FONT-FAMILY: arial,sans-serif">长度必须最少包含 6 个字符。</span>
<table cellSpacing=0 cellPadding=0 border=0>
<td vAlign=top noWrap width=0><font face="Arial, sans-serif" size=-1>密码强度: </font></td>
<td vAlign=top noWrap><font face="Arial, sans-serif" color=#808080 size=-1><strong>
<div id=passwdRating></div>
</strong></font></td>
<tr>
<td height=3></td>
</tr>
<tr>
<td colSpan=2>
<table id=passwdBar cellSpacing=0 cellPadding=0 width=180 bgColor=#ffffff border=0>
<td id=posBar width=0% bgColor=#e0e0e0 height=4></td>
<td id=negBar width="100%" bgColor=#e0e0e0 height=4></td>
</table>
</td>
</tr>
</table>
</td>
       

<script>

   ratingMsgs = new Array(6);
   ratingMsgColors = new Array(6);
   barColors = new Array(6);
   ratingMsgs[0] = "太短";
   ratingMsgs[1] = "弱";
   ratingMsgs[2] = "一般";
   ratingMsgs[3] = "很好";
   ratingMsgs[4] = "极佳";
   ratingMsgs[5] = "未评级"; //If the password server is down
   ratingMsgColors[0] = "#676767";
   ratingMsgColors[1] = "#aa0033";
   ratingMsgColors[2] = "#f5ac00";
   ratingMsgColors[3] = "#6699cc";
   ratingMsgColors[4] = "#008000";
   ratingMsgColors[5] = "#676767";
   barColors[0] = "#dddddd";
   barColors[1] = "#aa0033";
   barColors[2] = "#ffcc33";
   barColors[3] = "#6699cc";
   barColors[4] = "#008000";
   barColors[5] = "#676767";
   function CreateRatePasswdReq(pwd) {
     if (!isBrowserCompatible) {
       return;
     }
    
    // if(!document.getElementById) return false;
    // var pwd = document.getElementById("pwd");
     if(!pwd) return false;  
     passwd=pwd.value;
     var min_passwd_len = 7;   //注意这是控制密码长度的
     if (passwd.length < min_passwd_len)   {
       if (passwd.length > 0) {
         DrawBar(0);
       } else {
         ResetBar();
       }
     } else {
      //We need to escape the password now so it won't mess up with length test
        rating = checkPasswdRate(passwd);
        DrawBar(rating);
  
     }
   }
function getElement(name) {
     if (document.all) {
         return document.all(name);
     }
     return document.getElementById(name);
}
  
   function DrawBar(rating) {
     var posbar = getElement('posBar');
     var negbar = getElement('negBar');
     var passwdRating = getElement('passwdRating');
     var barLength = getElement('passwdBar').width;
     if (rating >= 0 && rating <= 4) {   //We successfully got a rating
       posbar.style.width = barLength / 4 * rating + "px";
       negbar.style.width = barLength / 4 * (4 - rating) + "px";
     } else {
       posbar.style.width = "0px";
       negbar.style.width = barLength + "px";
       rating = 5; // Not rated Rating
     }
     posbar.style.background = barColors[rating];
     passwdRating.innerHTML = "<font color='" + ratingMsgColors[rating] +
                              "'>" + ratingMsgs[rating] + "</font>";
   }
   
   //Resets the password strength bar back to its initial state without any message showing.
   function ResetBar() {
     var posbar = getElement('posBar');
     var negbar = getElement('negBar');
     var passwdRating = getElement('passwdRating');
     var barLength = getElement('passwdBar').width;
     posbar.style.width = "0px";
     negbar.style.width = barLength + "px";
     passwdRating.innerHTML = "";
   }
   /* Checks Browser Compatibility */
   var agt = navigator.userAgent.toLowerCase();
   var is_op = (agt.indexOf("opera") != -1);
   var is_ie = (agt.indexOf("msie") != -1) && document.all && !is_op;
   var is_mac = (agt.indexOf("mac") != -1);
   var is_gk = (agt.indexOf("gecko") != -1);
   var is_sf = (agt.indexOf("safari") != -1);
   function gff(str, pfx) {
     var i = str.indexOf(pfx);
     if (i != -1) {
       var v = parseFloat(str.substring(i + pfx.length));
       if (!isNaN(v)) {
       return v;
       }
     }
     return null;
   }
   function Compatible() {
     if (is_ie && !is_op && !is_mac) {
       var v = gff(agt, "msie ");
       if (v != null) {
         return (v >= 6.0);
       }
     }
     if (is_gk && !is_sf) {
       var v = gff(agt, "rv:");
       if (v != null) {
          return (v >= 1.4);
       } else {
          v = gff(agt, "galeon/");
          if (v != null) {
            return (v >= 1.3);
          }
       }
     }
     if (is_sf) {
       var v = gff(agt, "applewebkit/");
       if (v != null) {
         return (v >= 124);
       }
     }
     return false;
   }
  
   /* We also try to create an xmlhttp object to see if the browser supports it */
   var isBrowserCompatible = Compatible();

//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 checkPasswdRate(sPW){  
if (sPW.length<=4)  
return 0; //密码太短  
Modes=0;  
for (i=0;i<sPW.length;i++){  
//测试每一个字符的类别并统计一共有多少种模式.  
Modes|=CharMode(sPW.charCodeAt(i));  
}  
return bitTotal(Modes);  
}  
  

</script>


</body>
</html>

大部分是从GOOGLE中找到的代码

但有问题在于在ASP。NET中无法作为服务器控件使用

一旦作为服务器控件使用后 不执行JS代码了

还有JS代码必须写在页面

引用会出错 期待解决方案

在ASP。NET中 如何判断两个密码是否相同

不知道谁有好方法

我百度一下 找到一个

但有点小问题 密码自动多了个“逗号”

但至少能读取出来

自己稍加处理应该可以用

代码如下 我是在一个按扭下写的代码

string str = Convert.ToString(Request.Form["pwd"]);
         Label5.Text = str;

注意这里的PWD 是INPUT控件的NAME

上面的代码没写入

大家自己加上

否则就只能成个样式了

呵呵   期待大家帮我解决那几个问题

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值