1.需求
- 包含一种~弱
- 包含两种~中
- 包含三种~强
逻辑
- 拿到密码
- 准备正则(符合三个强度的那些)
- 根据密码强度决定高亮哪几个span标签
代码
- html + css
<form>
<label>
密码 : <input type="text">
<p>
<span class="r">弱</span>
<span class="z">中</span>
<span class="q">强</span>
</p>
</label>
</form>
<style>
* {
margin: 0;
padding: 0;
}
form {
width: 500px;
padding: 20px;
border: 5px solid pink;
border-radius: 15px;
margin: 50px auto;
}
label {
font-size: 20px;
}
label>input {
font-size: 20px;
padding-left: 20px;
}
label>p {
height: 30px;
display: flex;
justify-content: space-between;
margin-top: 20px;
}
label>p>span {
width: 30%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: #ccc;
color: #fff;
}
label>p>span:nth-child(1).active {
background-color: red;
}
label>p>span:nth-child(2).active {
background-color: orange;
}
label>p>span:nth-child(3).active {
background-color: green;
}
</style>
- js
const inp = document.querySelector('input')
const spans = [...document.querySelectorAll('span')]
inp.oninput = function(){
const value = this.value
const reg1 = /\d/
const reg2 = /[a-zA-Z]/
const reg3 = /[!@#]/
let level = 0
if(reg1.test(value)){
level++
}
if(reg2.test(value)){
level++
}
if(reg3.test(value)){
level++
}
for(let i = 0; i < spans.length; i++){
spans[i].className = ''
if(i < level){
span[i].className = 'active'
}
}
}