六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 105|回复: 0

JS要来自动判断密码的安全强度

[复制链接]

升级  30.33%

87

主题

87

主题

87

主题

举人

Rank: 3Rank: 3

积分
291
 楼主| 发表于 2013-2-7 19:17:58 | 显示全部楼层 |阅读模式
先看效果

 


代码<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>用于判断密码的安全强度</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><script 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="#eeeeee"; L_color="#FF4040"; 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; } </script>  </head>    <body>   <table>   <tr id ="tr_Password2">          <td height="30" align="right" >密码:</td>          <td>          <input type="password" name="password" class="input1" onKeyUp="pwStrength(this.value)" >          <font color="red">*</font>          </td>        </tr>        <tr id ="tr_ConfirmPassword2">          <td height="30" align="right" >确认密码:</td>          <td><input type="password" name="passCf" class="input1">          <font color="red">*</font>          </td>        </tr>        <tr>        <td align="right">密码强度:</td>        <td><table  border="1" cellspacing="0" cellpadding="1" height="20" style="border-color: #5b5b5b;"> <tr align="center" bgcolor="#eeeeee"> <td width="33%" id="strength_L">弱</td> <td width="33%" id="strength_M">中</td> <td width="33%" id="strength_H">安全</td> </tr> </table>                </td>        </tr>    </table>  </body></html> 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表