现在的位置: 首页 > 综合 > 正文

js实现密码强度检测

2014年01月31日 ⁄ 综合 ⁄ 共 2596字 ⁄ 字号 评论关闭
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
      
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
      
  5. %>   
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html>   
  9.   <head>   
  10.     <base href="<%=basePath%>">   
  11.        
  12.     <title>密码强度检测实例</title>   
  13.        
  14.     <meta http-equiv="pragma" content="no-cache">   
  15.     <meta http-equiv="cache-control" content="no-cache">   
  16.     <meta http-equiv="expires" content="0">       
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  18.     <meta http-equiv="description" content="This is my page">   
  19.     <script type='text/javascript' src='js/jquery-latest.js'></script>   
  20.     <script language=javascript>    
  21.     //CharMode函数    
  22.     //测试某个字符是属于哪一类.    
  23.     function CharMode(iN){    
  24.     if (iN>=48 && iN <=57) //数字    
  25.     return 1;    
  26.     if (iN>=65 && iN <=90) //大写字母    
  27.     return 2;    
  28.     if (iN>=97 && iN <=122) //小写    
  29.     return 4;    
  30.     else    
  31.     return 8; //特殊字符    
  32.     }    
  33.        
  34.     //bitTotal函数    
  35.     //计算出当前密码当中一共有多少种模式    
  36.     function bitTotal(num){    
  37.         modes=0;    
  38.         for (i=0;i<4;i++){    
  39.             if (num & 1){   
  40.                 modes++;   
  41.             }    
  42.             num>>>=1;    
  43.             }    
  44.         return modes;    
  45.     }    
  46.        
  47.     //checkStrong函数    
  48.     //返回密码的强度级别    
  49.        
  50.     function checkStrong(sPW){    
  51.         if (sPW.length<=4){   
  52.             return 0; //密码太短    
  53.             }   
  54.             Modes=0;    
  55.             for (i=0;i<sPW.length;i++){    
  56.                 //测试每一个字符的类别并统计一共有多少种模式.    
  57.                 Modes|=CharMode(sPW.charCodeAt(i));    
  58.             }    
  59.            
  60.         return bitTotal(Modes);    
  61.        
  62.     }    
  63.        
  64.     //pwStrength函数    
  65.     //当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的级别   
  66.        
  67.     function pwStrength(pwd){    
  68.     if (pwd==null||pwd==''){    
  69.         var result='安全系数危险!';   
  70.     }    
  71.     else{    
  72.         S_level=checkStrong(pwd);    
  73.         switch(S_level) {    
  74.             case 0:    
  75.                 var result='安全系数危险!';   
  76.             case 1:    
  77.                 var result='安全系数低!';    
  78.                 break;    
  79.             case 2:    
  80.                 var result='安全系数中!';    
  81.                 break;    
  82.             default:    
  83.                 var result='安全系数高!';    
  84.         }    
  85.     }    
  86.     $("#result").html(result);   
  87.     return;    
  88.     }    
  89. </script>    
  90.   
  91.   
  92.   </head>   
  93.      
  94.   <body>   
  95.     <form name=form1 action="" >    
  96.     输入密码: :<input type=password size=10 onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value)/>    
  97.     <br>密码强度:    
  98.     <div id="result"></div>   
  99.     </form>    
  100.   </body>   
  101. </html>  
【上篇】
【下篇】

抱歉!评论已关闭.