找回密码
 注册加入

扫一扫,极速登录

QQ登录

只需一步,快速开始

搜索
查看: 6467|回复: 0

检查密码强度的代码

[复制链接]
发表于 2012-7-12 07:06:24 | 显示全部楼层 |阅读模式
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  5. <title>无标题文档</title>
  6. <script language=javascript>

  7. //CharMode函数
  8. //测试某个字符是属于哪一类.
  9. function CharMode(iN){
  10. if (iN>=48 && iN <=57) //数字
  11. return 1;
  12. if (iN>=65 && iN <=90) //大写字母
  13. return 2;
  14. if (iN>=97 && iN <=122) //小写
  15. return 4;
  16. else
  17. return 8; //特殊字符
  18. }
  19. //bitTotal函数
  20. //计算出当前密码当中一共有多少种.
  21. function bitTotal(num){
  22. modes=0;
  23. for (i=0;i<4;i++){
  24. if (num & 1) modes++;
  25. num>>>=1;
  26. }
  27. return modes;
  28. }
  29. //checkStrong函数
  30. //返回密码的强度级别
  31. function checkStrong(sPW){
  32. if (sPW.length<=4)
  33. return 0; //密码太短
  34. Modes=0;
  35. for (i=0;i<sPW.length;i++){
  36. //测试每一个字符的类别并统计一共有多少种模式.
  37. Modes|=CharMode(sPW.charCodeAt(i));
  38. }
  39. return bitTotal(Modes);
  40. }
  41. //pwStrength函数
  42. //当用户放开键盘或密码输入框失去焦点时,根据不同的级别显示不同的颜色
  43. function pwStrength(pwd){
  44. O_color="#eeeeee";
  45. L_color="#FF0000";
  46. M_color="#FF9900";
  47. H_color="#33CC00";
  48. if (pwd==null||pwd==''){
  49. Lcolor=Mcolor=Hcolor=O_color;
  50. }
  51. else{
  52. S_level=checkStrong(pwd);
  53. switch(S_level) {
  54. case 0:
  55. Lcolor=Mcolor=Hcolor=O_color;
  56. case 1:
  57. Lcolor=L_color;
  58. Mcolor=Hcolor=O_color;
  59. break;
  60. case 2:
  61. Lcolor=Mcolor=M_color;
  62. Hcolor=O_color;
  63. break;
  64. default:
  65. Lcolor=Mcolor=Hcolor=H_color;
  66. }
  67. }
  68. document.getElementById("strength_L").style.background=Lcolor;
  69. document.getElementById("strength_M").style.background=Mcolor;
  70. document.getElementById("strength_H").style.background=Hcolor;
  71. return;
  72. }
  73. </script>
  74. </head>
  75. <body>
  76. <form name=form1 action="" >
  77. 输入密码:<input type=password size=50 onKeyUp=pwStrength(this.value) onBlur=pwStrength(this.value)>

  78. 密码强度:
  79. <table width="217" border="1" cellspacing="0" cellpadding="1" bordercolor="#cccccc" height="23" style='display:inline'>
  80. <tr align="center" bgcolor="#eeeeee">
  81. <td width="33%" id="strength_L">弱</td>
  82. <td width="33%" id="strength_M">中</td>
  83. <td width="33%" id="strength_H">强</td>
  84. </tr>
  85. </table>
  86. </form>
  87. </body>
  88. </html>
复制代码
您需要登录后才可以回帖 登录 | 注册加入  

本版积分规则

Archiver|手机版|小黑屋|Discuz!扩展中心 ( 浙ICP备14042422号-1 )|网站地图QQ机器人

GMT+8, 2024-4-19 20:42 , Processed in 0.142441 second(s), 13 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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