需求:
1.需要對用戶輸入的密碼進行驗證,驗證的級別分爲強中弱,如果輸入的密碼強度少於6時,則不會驗證,只有密碼強度在6-20時纔會進行驗證。
相關的正則
//密碼爲八位及以上並且字母數字特殊字符三項都包括
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
//密碼爲七位及以上並且字母、數字、特殊字符三項中有兩項,強度是中等
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
2.密碼的顯示與隱藏,點擊小眼可以對密碼進行顯示或者隱藏。
具體代碼
html部分:
<div class="wrapper">
<div class="input_box">
<input type="password" name="" placeholder="請輸入密碼" oninput="passValidate(this)" id="inputPwd" value="">
<div class="eye_icon"></div>
</div>
<p>請至少使用字母、數字、符號兩種類型組合的密碼,長度爲6~20位。</p>
<ul class="pwdStrength">
<li class="weak"></li>
<li class="middle"></li>
<li class="strong"></li>
<li class="result"></li>
</ul>
</div>
css部分:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.wrapper {
width: 500px;
height: 200px;
border: 1px solid #eee;
margin: 100px auto;
display: flex;
align-items: center;
flex-direction: column;
}
.input_box{
width: 80%;
display: flex;
align-items: center;
}
.input_box input {
width: 82%;
height: 30px;
border: none;
outline: none;
border: 1px solid #D2B48C;
border-radius: 12px;
margin: 10px 0px;
padding-left: 15px;
}
.eye_icon{
width: 20px;
height: 20px;
background-image: url('./open_eye.png');
background-repeat: no-repeat;
background-position: center content;
background-size: cover;
margin-left: 10px;
}
.wrapper p {
width: 80%;
height: 60px;
line-height: 26px;
font-size: 14px;
color: #339999;
}
.pwdStrength {
width: 80%;
list-style: none;
height: 30px;
display: none;
flex: 1;
}
.weak,
.middle,
.strong {
height: 15px;
width: 30px;
border: 1px solid black;
background: rgb(238, 238, 238);
}
.middle {
border-left: 0;
border-right: 0;
}
.result {
width: 30px;
height: 15px;
font-size: 14px;
line-height: 14px;
text-align: center;
margin-left: 10px;
}
JS部分:
//密碼的可見與隱藏、
console.log($('#inputPwd'))
var eyeFlag = false;
$('.eye_icon').click(function(){
if(!eyeFlag){
$(this).css({'background-image': 'url(' + "./close_eye.png" + ')'});
$('#inputPwd').attr('type','text');
}else{
$(this).css({'background-image': 'url(' + "./open_eye.png" + ')'});
$('#inputPwd').attr('type','password')
}
eyeFlag = !eyeFlag;
})
//密碼強度驗證
function passValidate(e) {
var pwd = $.trim(e.value);
if (pwd === '') {
$('.pwdStrength').css({'display':'none'})
$('.weak').css({
'background': 'rgb(238, 238, 238)'
});
$('.middle').css({
'background': 'rgb(238, 238, 238)'
});
$('.strong').css({
'background': 'rgb(238, 238, 238)'
});
$('.result').text('')
} else {
$('.pwdStrength').css({'display':'flex'})
//密碼爲八位及以上並且字母數字特殊字符三項都包括
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
//密碼爲七位及以上並且字母、數字、特殊字符三項中有兩項,強度是中等
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
var enoughRegex = new RegExp("(?=.{6,}).*", "g");
if (false == enoughRegex.test(pwd)) {
} else if (strongRegex.test(pwd)) {
$('.strong').css({
'background': '#33ff33'
});
$('.result').text('強')
} else if (mediumRegex.test(pwd)) {
$('.middle').css({
'background': '#FFC125'
});
$('.strong').css({
'background': 'rgb(238, 238, 238)'
});
$('.result').text('中')
} else {
$('.weak').css({
'background': '#EE4000'
});
$('.middle').css({
'background': 'rgb(238, 238, 238)'
});
$('.strong').css({
'background': 'rgb(238, 238, 238)'
});
$('.result').text('弱')
}
}
}
效果
密碼強度爲弱
密碼強度爲中:
密碼強度爲強