返回列表 发帖

检查密码强度的代码

检查密码强度的代码
  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. <br>密码强度:

  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>
复制代码
激情, 创新, 进取! http://www.chuangxin.biz

返回列表

站长推荐 关闭


云南珠宝网 - 翡翠原石、毛料批发零售

云南珠宝网 - 专业翡翠原石、毛料批发零售电子商务网。新店开张在本站购物一例免费快递。


查看