密码强度的实现

2019-05-05 11:05:32 浏览数 (1)

3.密码强度的实现

如果开发中遇到这样的功能,完成可以照搬ecshop4这个功能

它的是原理:根据Unicode 编码来判断密码的强弱。

代码语言:txt复制
        <tr>
          <td align="right">{$lang.label_password}</td>
          <td>
          <input name="password" type="password" id="password1" onblur="check_password(this.value);" 
              onkeyup="checkIntensity(this.value)" class="inputBg" style="width:179px;" />
            <span style="color:#FF0000" id="password_notice"> *</span>
          </td>
        </tr>
        <tr>
          <td align="right">{$lang.label_password_intensity}</td>
          <td>
            <table width="145" border="0" cellspacing="0" cellpadding="1">
              <tr align="center">
                <td width="33%" id="pwd_lower">{$lang.pwd_lower}</td>
                <td width="33%" id="pwd_middle">{$lang.pwd_middle}</td>
                <td width="33%" id="pwd_high">{$lang.pwd_high}</td>
              </tr>
            </table>
          </td>
        </tr>

checkIntensity(this.value)这个函数就是判断密码强度,它写在user.js里面

代码语言:txt复制
/* *
 * 检测密码强度
 * @param       string     pwd     密码
 */
function checkIntensity(pwd)
{
  var Mcolor = "#FFF",Lcolor = "#FFF",Hcolor = "#FFF";//定义三种颜色,强、中、弱
  var m=0;

  var Modes = 0;
  for (i=0; i<pwd.length; i  )
  {
    var charType = 0;
    var t = pwd.charCodeAt(i); //得出Unicode 编码编码,值越大,密码就越复杂
    if (t>=48 && t <=57)
    {
      charType = 1;
    }
    else if (t>=65 && t <=90)
    {
      charType = 2;
    }
    else if (t>=97 && t <=122)
      charType = 4;
    else
      charType = 4;
    Modes |= charType;
  }

  for (i=0;i<4;i  )
  {
    if (Modes & 1) m  ;
      Modes>>>=1;
  }

  if (pwd.length<=4)
  {
    m = 1;
  }

  switch(m)
  {
    case 1 :
      Lcolor = "2px solid red";
      Mcolor = Hcolor = "2px solid #DADADA";
    break;
    case 2 :
      Mcolor = "2px solid #f90";
      Lcolor = Hcolor = "2px solid #DADADA";
    break;
    case 3 :
      Hcolor = "2px solid #3c0";
      Lcolor = Mcolor = "2px solid #DADADA";
    break;
    case 4 :
      Hcolor = "2px solid #3c0";
      Lcolor = Mcolor = "2px solid #DADADA";
    break;
    default :
      Hcolor = Mcolor = Lcolor = "";
    break;
  }
  if (document.getElementById("pwd_lower"))
  {
    document.getElementById("pwd_lower").style.borderBottom  = Lcolor;
    document.getElementById("pwd_middle").style.borderBottom = Mcolor;
    document.getElementById("pwd_high").style.borderBottom   = Hcolor;
  }


}

0 人点赞