1. <div id="dv">
    2. <label for="pwd">密码</label>
    3. <input type="text" id="pwd" maxlength="16"><!--课外话题-->
    4. <div>
    5. <em>密码强度:</em>
    6. <em id="strength"></em>
    7. <div id="strengthLevel" class="strengthLv0"></div>
    8. </div>
    9. </div>
    10. <script src="common.js"></script>
    11. <style type="text/css">
    12. #dv{
    13. width: 300px;
    14. height:200px;
    15. position: absolute;
    16. left:300px;
    17. top:100px;
    18. }
    19. .strengthLv0 {
    20. height: 6px;
    21. width: 120px;
    22. border: 1px solid #ccc;
    23. padding: 2px;
    24. }
    25. .strengthLv1 {
    26. background: red;
    27. height: 6px;
    28. width: 40px;
    29. border: 1px solid #ccc;
    30. padding: 2px;
    31. }
    32. .strengthLv2 {
    33. background: orange;
    34. height: 6px;
    35. width: 80px;
    36. border: 1px solid #ccc;
    37. padding: 2px;
    38. }
    39. .strengthLv3 {
    40. background: green;
    41. height: 6px;
    42. width: 120px;
    43. border: 1px solid #ccc;
    44. padding: 2px;
    45. }
    46. .strengthLv4 {
    47. background: black;
    48. height: 6px;
    49. width: 150px;
    50. border: 1px solid #ccc;
    51. padding: 2px;
    52. }
    53. </style>
    54. my$("pwd").onkeyup = function(){
    55. var value = this.value;
    56. var level = 0;
    57. //1.如果输入的内容中函数字 强度是0
    58. if(/\d+/.test(value)){
    59. level++;
    60. }
    61. //2.如果输入的内容含小写字母 强度是1
    62. if(/[a-z]+/.test(value)){
    63. level++;
    64. }
    65. //3.如果输入的内容含大写字母 强度是2
    66. if(/[A-Z]+/.test(value)){
    67. level++;
    68. }
    69. //4.如果输入的内容含非字符 强度是3
    70. if(/\W+/.test(value)){
    71. level++;
    72. }
    73. my$("strengthLevel").className = "strengthLv"+level;
    74. }

    test.gif