1. <script type="text/javascript">
  2.     var msg = ' Hello ';
  3.     msg = msg.replace(/^\s+/, "").replace(/&\s+/, '');
  4.     alert(msg);
  5. </script>
  1. <script type="text/javascript">
  2.     window.onload = function () {
  3.         //这里为文本框注册一个键盘键按下后弹起的事件
  4.         document.getElementById('txtPassword').onkeyup = function () {
  5.             var tds = document.getElementById('tb1').getElementsByTagName('td');
  6.             //初始化颜色
  7.             for (var i = 0; i <tds.length; i++) {
  8.                 tds[i].style.backgroundColor = '';
  9.             }
  10.             //1:获取用户输入的内容
  11.             var val = this.value;
  12.             if (val.length > 0) {
  13.             //2:根据用户输入的密码,来效验密码的强度。
  14.             var pwdLvl = getPasswordLever(val);
  15.             //3:改变下面显示的强度。
  16.             switch (pwdLvl) {
  17.                 case 0:
  18.                 case 1:
  19.                 case 2:
  20.                     //弱
  21.                     tds[0].style.backgroundColor = 'red';
  22.                     break;
  23.                 case 3:
  24.                 case 4:
  25.                     //中
  26.                     tds[0].style.backgroundColor = 'orange';
  27.                     tds[1].style.backgroundColor = 'orange';
  28.                     break;
  29.                 case 5:
  30.                     //强
  31.                     tds[0].style.backgroundColor = 'green';
  32.                     tds[1].style.backgroundColor = 'green';
  33.                     tds[2].style.backgroundColor = 'green';
  34.                     break;
  35.                 default:
  36.             }
  37.             }
  38.  
  39.         };
  40.     };
  41.     //这里是申明了一个匿名函数,通过这个匿名函数来判断密码的强度。
  42.     function getPasswordLever(user_pwd) {
  43.         //下面是通过正则表达式的match()方法来判断输入的密码中是否有满足的指定条件。
  44.         var lvl = 0;
  45.         //1:含有数字,强度+1
  46.         if (user_pwd.match(/\d+/)) {
  47.             lvl++;
  48.         }
  49.         //2:含有小写字母,强度+1
  50.         if (user_pwd.match(/[a-z]+/)) {
  51.             lvl++;
  52.         }
  53.         //3:含有大写字母,强度+1
  54.         if (user_pwd.match(/[A-Z]+/)) {
  55.             lvl++;
  56.         }
  57.         //4:含有特殊符号,强度+1
  58.         if (user_pwd.match(/[^0-9a-zA-Z]+/)) {
  59.             lvl++;
  60.         }
  61.         //5:密码的长度超过6位,强度+1
  62.         if (user_pwd.length > 6) {
  63.             lvl++;
  64.         }
  65.         return lvl;
  66.     }
  67. </script>

JavaScript基础插曲-练习-LMLPHP

04-28 04:29