用于登录等表单中检验用户输入的密码强度。

基础用法
<password-strength :password="password">password-strength>
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| password | 密码 | String | - |
| 事件名 | 说明 | 返回值 |
|---|---|---|
| on-change | 密码强度改变监听 | strength (1:低、2:中、3:高) |
<template>
<div class="password-strength">
<div class="password-strength-tip">
密码强度:<b>{{ strength === 3 ? '高' : (strength === 2 ? '中' : '低') }}b><span v-show="strength === 2">(推荐)span>
div>
<ul class="password-strength-bar">
<li :class="{ active: strength >= 1 }">li>
<li :class="{ active: strength >= 2 }">li>
<li :class="{ active: strength === 3 }">li>
ul>
div>
template>
<script>
export default {
name: 'password-strength',
props: {
password: {
type: String,
default: ''
}
},
data() {
return {
strength: 0
}
},
watch: {
password: {
handler(newVal) {
if (!newVal) {
this.strength = 0
this.$emit('on-change', this.strength)
return
} else if (newVal.length < 8) {
this.strength = 1
this.$emit('on-change', this.strength)
return
}
let hasNum = 0
let hasWord = 0
let hasSpecChar = 0
for (let i = 0; i < newVal.length; i++) {
if (hasNum === 0 && /[0-9]/.test(newVal[i])) {
hasNum++
} else if (hasWord === 0 && /[a-zA-Z]/.test(newVal[i])) {
hasWord++
} else if (hasSpecChar === 0 && /[@#¥!^&*()]/.test(newVal[i])) {
hasSpecChar++
}
}
if (hasNum + hasWord + hasSpecChar < 2) {
this.strength = 1
} else if (hasNum + hasWord + hasSpecChar === 2) {
this.strength = 2
} else if (hasNum + hasWord + hasSpecChar === 3) {
this.strength = 3
}
this.$emit('on-change', this.strength)
}
}
}
}
script>
<style scoped lang="scss">
.password-strength {
width: 100%;
margin-bottom: 16px;
.password-strength-tip {
display: flex;
align-items: center;
margin-bottom: 8px;
font-size: 12px;
span {
color: #666;
}
}
.password-strength-bar {
display: flex;
li {
flex: 1;
height: 4px;
border-radius: 4px;
opacity: .2;
transition: all .2s linear;
& + li {
margin-left: 2px;
}
&.active {
opacity: 1;
}
&:first-child {
background: #FF0000;
}
&:nth-child(2) {
background: #FF7200;
}
&:last-child {
background: #3FD662;
}
}
}
}
style>