Skip to content

Commit a04a896

Browse files
committed
feat: add password strength indicators and validation to user form
1 parent ddfc4b2 commit a04a896

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

‎apps/daas/src/i18n/langs/en.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,9 @@ export default {
888888
'The length of the password cannot be less than 5 and greater than 32',
889889
user_form_password_not_cn:
890890
'Password only allows English, numbers and English punctuation',
891+
user_password_strength_weak: 'Weak',
892+
user_password_strength_medium: 'Medium',
893+
user_password_strength_strong: 'Strong',
891894
user_form_activation_code: 'Access Code',
892895
user_form_status: 'Status',
893896
cluster_name: 'Monitor Name',

‎apps/daas/src/i18n/langs/zh-CN.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,9 @@ export default {
837837
user_form_password_null: '请输入密码, 长度为 5 ~ 32 个字符',
838838
user_form_pass_hint: '密码长度不能小于5大于32',
839839
user_form_password_not_cn: '密码仅允许英文、数字和英文标点符号',
840+
user_password_strength_weak: '弱',
841+
user_password_strength_medium: '中',
842+
user_password_strength_strong: '强',
840843
user_form_activation_code: '访问码',
841844
user_form_status: '状态',
842845
cluster_name: '监控名称',

‎apps/daas/src/i18n/langs/zh-TW.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,9 @@ export default {
833833
user_form_password_null: '請輸入密碼, 長度為 5 ~ 32 個字符',
834834
user_form_pass_hint: '密碼長度不能小於5大於32',
835835
user_form_password_not_cn: '密碼僅允許英文、數字和英文標點符號',
836+
user_password_strength_weak: '弱',
837+
user_password_strength_medium: '中',
838+
user_password_strength_strong: '強',
836839
user_form_activation_code: '訪問碼',
837840
user_form_status: '狀態',
838841
cluster_name: '監控名稱',

‎apps/daas/src/views/user/List.vue‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,30 @@ export default {
191191
message: this.$t('account_user_null'),
192192
},
193193
],
194+
password: [
195+
{
196+
required: true,
197+
validator: (rule, v, callback) => {
198+
if (
199+
this.createForm.id && // 编辑时密码选填,但填了就要校验
200+
!v
201+
)
202+
return callback()
203+
if (!v || !v.trim()) {
204+
return callback(new Error(this.$t('user_form_password_null')))
205+
} else if (v.length < 5 || v.length > 32) {
206+
return callback(new Error(this.$t('user_form_pass_hint')))
207+
} else if (/[\s\u4E00-\u9FA5]/.test(v)) {
208+
return callback(
209+
new Error(this.$t('user_form_password_not_cn')),
210+
)
211+
} else {
212+
return callback()
213+
}
214+
},
215+
trigger: ['blur', 'change'],
216+
},
217+
],
194218
email: [
195219
{
196220
required: true,
@@ -220,6 +244,19 @@ export default {
220244
table() {
221245
return this.$refs.table
222246
},
247+
passwordStrength() {
248+
const v = this.createForm.password
249+
if (!v) return 0
250+
let score = 0
251+
if (v.length >= 5) score++
252+
if (v.length >= 8) score++
253+
if (/[A-Z]/.test(v)) score++
254+
if (/\d/.test(v)) score++
255+
if (/[^A-Z0-9]/i.test(v)) score++
256+
if (score <= 1) return 1 // 弱
257+
if (score <= 3) return 2 // 中
258+
return 3 // 强
259+
},
223260
},
224261
watch: {
225262
'$route.query': function () {
@@ -1092,6 +1129,28 @@ export default {
10921129
maxlength="32"
10931130
show-password
10941131
/>
1132+
<div v-if="createForm.password" class="password-strength mt-1">
1133+
<div class="password-strength__bars">
1134+
<span
1135+
v-for="i in 3"
1136+
:key="i"
1137+
class="password-strength__bar"
1138+
:class="`password-strength__bar--${passwordStrength >= i ? ['weak', 'medium', 'strong'][passwordStrength - 1] : 'empty'}`"
1139+
/>
1140+
</div>
1141+
<span
1142+
class="password-strength__label"
1143+
:class="`password-strength__label--${['weak', 'medium', 'strong'][passwordStrength - 1]}`"
1144+
>
1145+
{{
1146+
[
1147+
$t('user_password_strength_weak'),
1148+
$t('user_password_strength_medium'),
1149+
$t('user_password_strength_strong'),
1150+
][passwordStrength - 1]
1151+
}}
1152+
</span>
1153+
</div>
10951154
</el-form-item>
10961155
10971156
<!-- 角色 -->
@@ -1240,4 +1299,54 @@ export default {
12401299
}
12411300
}
12421301
}
1302+
1303+
.password-strength {
1304+
display: flex;
1305+
align-items: center;
1306+
gap: 8px;
1307+
width: 100%;
1308+
line-height: 20px;
1309+
1310+
&__bars {
1311+
display: flex;
1312+
gap: 4px;
1313+
flex: 1;
1314+
}
1315+
1316+
&__bar {
1317+
flex: 1;
1318+
height: 4px;
1319+
border-radius: 2px;
1320+
transition: background-color 0.3s;
1321+
1322+
&--empty {
1323+
background-color: var(--el-border-color);
1324+
}
1325+
&--weak {
1326+
background-color: #f56c6c;
1327+
}
1328+
&--medium {
1329+
background-color: #e6a23c;
1330+
}
1331+
&--strong {
1332+
background-color: #67c23a;
1333+
}
1334+
}
1335+
1336+
&__label {
1337+
font-size: 12px;
1338+
min-width: 16px;
1339+
text-align: right;
1340+
1341+
&--weak {
1342+
color: #f56c6c;
1343+
}
1344+
&--medium {
1345+
color: #e6a23c;
1346+
}
1347+
&--strong {
1348+
color: #67c23a;
1349+
}
1350+
}
1351+
}
12431352
</style>

0 commit comments

Comments
 (0)