<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: 0;
padding: 0;
}
form{
width: 500px;
padding: 20px;
border: 3px solid gray;
margin: 50px auto;
}
form>label{
width: 100%;
height: 70px;
}
form>label>input{
width: 100%;
box-sizing: border-box;
padding-left: 30px;
font-size: 20px;
height: 50px;
}
form>p{
width: 100%;
margin-top: 10px;
display: flex;
justify-content: space-between;
}
form>p>span{
width: 30%;
background-color: #ccc;
color: #fff;
height: 30px;
font-size: 20px;
line-height: 30px;
text-align: center;
}
form>p>span:nth-child(1).active{
background-color: red;
}
form>p>span:nth-child(2).active{
background-color: orange;
}
form>p>span:nth-child(3).active{
background-color: green;
}
</style>
</head>
<body>
<form>
<label>
<input type="text"/>
</label>
<p>
<span>弱</span>
<span>中</span>
<span>强</span>
</p>
</form>
<script>
var oInput = document.querySelector("input")
var oSpan = document.querySelectorAll("span")
oInput.oninput = function(event){
// console.log(this.value)
// console.log(event.target.value)
var reg1 = /\d/
var reg2 = /[a-z]/i
var reg3 = /[!@#$%^&*()]/
var level = 0//每次重置level,1表示满足一个正则,2表示满足两个,3表示满足三个
if(reg1.test(this.value)) level++
if(reg2.test(this.value)) level++
if(reg3.test(this.value)) level++
// console.log(level)//根据1,2,3判断强弱
for(var i=0;i<oSpan.length;i++){
oSpan[i].classList.remove("active")
if(i<level){
oSpan[i].classList.add("active")
}
}
}
</script>
</body>
</html>
效果显示: