js判断密码强度(使用正则表达式)

 完整代码如下:

html:

<!DOCTYPE html>

<html lang="en" dir="ltr">
  <head>
    <meta charset="UTF-8">
    <title> 判断密码强度 </title>
    <link rel="stylesheet" href="style.css">
  </head>
<body>
  <div class="container">
    <div class="input-box">
      <i class="show_hide iconfont icon-yanjing_bi"></i>
      <input type="password" placeholder="请输入密码">
    </div>
    <div class="indicator">
      <div class="icon-text">
        <i class="error_icon"></i>
        <h6 class="text"></h6>
      </div>
    </div>
  </div>
  
  <script src="script.js"></script>
  
</body>
</html>

css:

@import url('//at.alicdn.com/t/font_2532986_wz8cwu1rlq.css');

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body{
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #06a880;
}
.container{
  position: relative;
  max-width: 460px;
  width: 100%;
  background: #fff;
  border-radius: 4px;
  padding: 30px;
  margin: 0 20px;
  box-shadow: 0 5px 10px rgba(0,0,0,0.2);
}
.container .input-box{
  position: relative;
}
.input-box .iconfont{
  font-size: 30px;
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: #A6A6A6;
  padding: 5px;
  cursor: pointer;
}
.input-box input{
  height: 60px;
  width: 100%;
  border: 2px solid #d3d3d3;
  border-radius: 4px;
  font-size: 18px;
  font-weight: 500;
  color: #333;
  outline: none;
  padding: 0 50px 0 16px;
}

.container .indicator{
  display: none;
}
.container .indicator.active{
  display: block;
  margin-top: 14px;
}
.indicator .icon-text{
  display: flex;
  align-items: center;
}
.icon-text .error_icon{
  margin-right: 8px;
}
.icon-text .text{
  font-size: 14px;
  font-weight: 500;
  letter-spacing: 1px;
}

js:

const input = document.querySelector("input"),
      showHide = document.querySelector(".show_hide"),
      indicator = document.querySelector(".indicator"),
      iconText = document.querySelector(".icon-text"),
      text = document.querySelector(".text");

// 隐藏或显示密码

showHide.addEventListener("click", () => {
  if (input.type === "password") {
    input.type = "text";
    showHide.classList.replace("icon-yanjing_bi", "icon-yanjing-shi");
  } else {
    input.type = "password";
    showHide.classList.replace("icon-yanjing-shi", "icon-yanjing_bi");
  }
});


// 正则判断密码强度

let alphabet = /[a-zA-Z]/, //大小写字母
    numbers = /[0-9]/, //数字0-9
    scharacters = /[!,@,#,$,%,^,&,*,?,_,(,),-,+,=,~]/; //特殊符号

input.addEventListener("keyup", () => {
  indicator.classList.add("active");

  let val = input.value;
  if (val.match(alphabet) || val.match(numbers) || val.match(scharacters)) {
    text.textContent = "密码强度较低";
    input.style.borderColor = "#FF6333";
    showHide.style.color = "#FF6333";
    iconText.style.color = "#FF6333";
  }
  if (val.match(alphabet) && val.match(numbers) && val.length >= 6) {
    text.textContent = "密码强度中等";
    input.style.borderColor = "#cc8500";
    showHide.style.color = "#cc8500";
    iconText.style.color = "#cc8500";
  }
  if (val.match(alphabet) && val.match(numbers) && val.match(scharacters) && val.length >= 8) {
    text.textContent = "密码强度较高";
    input.style.borderColor = "#22C32A";
    showHide.style.color = "#22C32A";
    iconText.style.color = "#22C32A";
  }

  if (val == "") {
    indicator.classList.remove("active");
    input.style.borderColor = "#A6A6A6";
    showHide.style.color = "#A6A6A6";
    iconText.style.color = "#A6A6A6";
  }
});

更多css小动画,js小案例见我的主页:

我的主页

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值