Skip to content

Commit 7eb7388

Browse files
msgill2017sendya
authored andcommitted
fixed register password inline validation
1 parent e10ce37 commit 7eb7388

File tree

4 files changed

+50
-30
lines changed

4 files changed

+50
-30
lines changed

src/locales/lang/en-US/user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default {
3636
'The password is not strong enough',
3737
'user.password.strength.strong': 'Strength: strong',
3838
'user.password.strength.medium': 'Strength: medium',
39+
'user.password.strength.low': 'Strength: low',
3940
'user.password.strength.short': 'Strength: too short',
4041
'user.confirm-password.required': 'Please confirm your password!',
4142
'user.phone-number.required': 'Please enter your phone number!',

src/locales/lang/zh-CN/user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default {
3434
'user.password.strength.msg': '密码强度不够 ',
3535
'user.password.strength.strong': '强度:强',
3636
'user.password.strength.medium': '强度:中',
37+
'user.password.strength.low': '强度:低',
3738
'user.password.strength.short': '强度:太短',
3839
'user.confirm-password.required': '请确认密码!',
3940
'user.phone-number.required': '请输入正确的手机号',

src/utils/util.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,31 @@ export function removeLoadingAnimate (id = '', timeout = 1500) {
6565
document.body.removeChild(document.getElementById(id))
6666
}, timeout)
6767
}
68+
export function scorePassword (pass) {
69+
let score = 0
70+
if (!pass) {
71+
return score
72+
}
73+
// award every unique letter until 5 repetitions
74+
const letters = {}
75+
for (let i = 0; i < pass.length; i++) {
76+
letters[pass[i]] = (letters[pass[i]] || 0) + 1
77+
score += 5.0 / letters[pass[i]]
78+
}
79+
80+
// bonus points for mixing it up
81+
const variations = {
82+
digits: /\d/.test(pass),
83+
lower: /[a-z]/.test(pass),
84+
upper: /[A-Z]/.test(pass),
85+
nonWords: /\W/.test(pass)
86+
}
87+
88+
let variationCount = 0
89+
for (var check in variations) {
90+
variationCount += (variations[check] === true) ? 1 : 0
91+
}
92+
score += (variationCount - 1) * 10
93+
94+
return parseInt(score)
95+
}

src/views/user/Register.vue

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
v-model="state.passwordLevelChecked">
1919
<template slot="content">
2020
<div :style="{ width: '240px' }" >
21-
<div :class="['user-register', passwordLevelClass]">强度:<span>{{ passwordLevelName }}</span></div>
21+
<div :class="['user-register', passwordLevelClass]">{{ $t(passwordLevelName) }}</div>
2222
<a-progress :percent="state.percent" :showInfo="false" :strokeColor=" passwordLevelColor " />
2323
<div style="margin-top: 10px;">
2424
<span>{{ $t('user.register.password.popover-message') }}
@@ -98,12 +98,13 @@
9898
<script>
9999
import { getSmsCaptcha } from '@/api/login'
100100
import { deviceMixin } from '@/store/device-mixin'
101+
import { scorePassword } from '@/utils/util'
101102
102103
const levelNames = {
103-
0: '',
104-
1: '',
105-
2: '',
106-
3: ''
104+
0: 'user.password.strength.short',
105+
1: 'user.password.strength.low',
106+
2: 'user.password.strength.medium',
107+
3: 'user.password.strength.strong'
107108
}
108109
const levelClass = {
109110
0: 'error',
@@ -151,37 +152,26 @@ export default {
151152
},
152153
methods: {
153154
handlePasswordLevel (rule, value, callback) {
154-
console.log('value form handlePassword level', value)
155155
if (value === '') {
156-
callback()
157-
} else {
158-
console.log('level inside else form handlePassword level', this.state.level)
159-
// 判断这个字符串中有没有数字
160-
if (/[0-9]/.test(value)) {
161-
this.state.level++
162-
}
163-
// 判断字符串中有没有字母
164-
if (/[a-zA-Z]/.test(value)) {
165-
this.state.level++
166-
}
167-
// 判断字符串中有没有特殊符号
168-
if (/[^0-9a-zA-Z_]/.test(value)) {
169-
this.state.level++
156+
return callback()
170157
}
171-
this.state.passwordLevel = this.state.level
172-
this.state.percent = this.state.level * 30
173-
if (this.state.level >= 2) {
174-
if (this.state.level >= 3) {
175-
this.state.percent = 100
158+
console.log('scorePassword ; ', scorePassword(value))
159+
if (value.length >= 6) {
160+
if (scorePassword(value) >= 30) {
161+
this.state.level = 1
176162
}
177-
callback()
178-
} else {
179-
if (this.state.level === 0) {
180-
this.state.percent = 10
163+
if (scorePassword(value) >= 60) {
164+
this.state.level = 2
181165
}
166+
if (scorePassword(value) >= 80) {
167+
this.state.level = 3
168+
}
169+
} else {
170+
this.state.level = 0
182171
callback(new Error(this.$t('user.password.strength.msg')))
183172
}
184-
}
173+
this.state.passwordLevel = this.state.level
174+
this.state.percent = this.state.level * 33
185175
},
186176
187177
handlePasswordCheck (rule, value, callback) {

0 commit comments

Comments
 (0)