密码复杂度校验函数

密码复杂度校验函数封装

当建立一个文件,封装校验函数

export function getPwdSecurityLevel(pwdStr) {
  let VERY_WEAK = '非常弱'
  let WEAK = '弱'
  let AVERAGE = '一般'
  let STRONG = '强'
  let VERY_STRONG = '非常强'
  let SECURE = '安全'
  let VERY_SECURE = '非常安全'

  let result = VERY_WEAK;
  if (pwdStr == null) {
    return result
  }
  let grade = 0
  let index = 0
  let pwdChars = pwdStr.split("")

  let numIndex = 0;
  let sLetterIndex = 0;
  let lLetterIndex = 0;
  let symbolIndex = 0;

  // 数字 48-57 A-Z 65 - 90 a-z 97 - 122 !"#$%&'()*+,-./ (ASCII码:33~47)
  // :;<=>?@ (ASCII码:58~64) [\]^_` (ASCII码:91~96) {|}~
  // (ASCII码:123~126)
  for (let i = 0; i < pwdStr.length; i++) {
    const ascll = pwdStr.charCodeAt(i);
    if (ascll >= 48 && ascll <= 57) {
      numIndex++;
    } else if (ascll >= 65 && ascll <= 90) {
      lLetterIndex++;
    } else if (ascll >= 97 && ascll <= 122) {
      sLetterIndex++;
    } else if ((ascll >= 33 && ascll <= 47)
      || (ascll >= 58 && ascll <= 64)
      || (ascll >= 91 && ascll <= 96)
      || (ascll >= 123 && ascll <= 126)) {
      symbolIndex++;
    }

  }

  // for (let i = 0; i < pwdChars.length; i++) {
  //   const ascll = pwdChars.charCodeAt(i);
  //   if (ascll >= 48 && ascll <= 57) {
  //     numIndex++;
  //   } else if (ascll >= 65 && ascll <= 90) {
  //     lLetterIndex++;
  //   } else if (ascll >= 97 && ascll <= 122) {
  //     sLetterIndex++;
  //   } else if ((ascll >= 33 && ascll <= 47)
  //     || (ascll >= 58 && ascll <= 64)
  //     || (ascll >= 91 && ascll <= 96)
  //     || (ascll >= 123 && ascll <= 126)) {
  //     symbolIndex++;
  //   }
  // }

  //一、密码长度: 5 分: 小于等于 4 个字符 10 分: 5 到 7 字符 25 分: 大于等于 8 个字符
  if (pwdChars.length <= 4) {
    index = 5;
  } else if (pwdChars.length <= 7) {
    index = 10;
  } else {
    index = 25;
  }
  grade += index;

//admin@123
  //二、字母: 0 分:没有字母,10 分:全都是小(大)写字母,20 分:大小写混合字母
  if (lLetterIndex === 0 && sLetterIndex === 0) {
    index = 0;
  } else if (lLetterIndex !== 0 && sLetterIndex !== 0) {
    index = 20;
  } else {
    index = 10;
  }
  grade += index;

  //三、数字: 0 分: 没有数字, 10 分: 1 个数字, 20 分: 大于 1 个数字
  if (numIndex === 0) {
    index = 0;
  } else if (numIndex === 1) {
    index = 10;
  } else {
    index = 20;
  }
  grade += index;

  // 四、符号: 0 分: 没有符号 10 分: 1 个符号 25 分: 大于 1 个符号
  if (symbolIndex === 0) {
    index = 0;
  } else if (symbolIndex === 1) {
    index = 10;
  } else {
    index = 25;
  }
  grade += index;

  // 五、奖励: 2 分: 字母和数字, 3 分: 字母、数字和符号, 5 分: 大小写字母、数字和符号
  if ((sLetterIndex !== 0 || lLetterIndex !== 0) && numIndex !== 0) {
    index = 2;
  } else if ((sLetterIndex !== 0 || lLetterIndex !== 0) && numIndex !== 0
    && symbolIndex !== 0) {
    index = 3;
  } else if (sLetterIndex !== 0 && lLetterIndex !== 0 && numIndex !== 0
    && symbolIndex !== 0) {
    index = 5;
  }
  else index = 0;
  grade += index;

  //最后的评分标准: >= 90: 非常安全 >= 80: 安全(Secure) >=
  // 50: 一般(Average) >= 25: 弱(Weak) >= 0: 非常弱

  if (grade >= 90) {
    result = VERY_SECURE;
  } else if (grade >= 80) {
    result = SECURE;
  } else if (grade >= 70) {
    result = VERY_STRONG;
  } else if (grade >= 60) {
    result = STRONG;
  } else if (grade >= 50) {
    result = AVERAGE;
  } else if (grade >= 25) {
    result = WEAK;
  } else if (grade >= 0) {
    result = VERY_WEAK;
  }
  return {result,grade};
}

需要使用校验密码处应用函数就可以啦!!!

效果展示

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的密码登录页面的 HTML 和 JavaScript 代码。它会验证用户名和密码是否与预设的值匹配,并给出相应的提示。 ```html <!DOCTYPE html> <html> <head> <title>密码登录</title> </head> <body> <h1>密码登录</h1> <form> <label for="username">用户名:</label> <input type="text" id="username" name="username"><br><br> <label for="password">密码:</label> <input type="password" id="password" name="password"><br><br> <button type="button" onclick="login()">登录</button> </form> <p id="message"></p> <script> function login() { var username = document.getElementById("username").value; var password = document.getElementById("password").value; if (username === "myusername" && password === "mypassword") { document.getElementById("message").innerHTML = "登录成功!"; } else { document.getElementById("message").innerHTML = "用户名或密码错误。"; } } </script> </body> </html> ``` 上面的代码中,我们使用了 HTML 表单元素来收集用户输入的用户名和密码。当用户点击登录按钮时,我们调用了 `login()` 函数来验证用户名和密码函数首先获取用户名和密码的值,然后将它们与预设的值进行比较。如果匹配,我们将在页面上显示“登录成功!”的消息;否则,我们将显示“用户名或密码错误。”的消息。 请注意,这只是一个简单的示例,实际上在生产环境中,你需要更严格的安全措施来确保密码的安全性,例如使用加密存储密码,限制登录尝试次数,设置密码复杂度要求等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值