需求:
1)需要输入原密码,如果输入的原密码不对,则给出相应提示;
2)新密码需要确认,输入两次,且相同,否则系统给出提示;
3)新密码长度大于等于6个字符小手18 个字符;
4)新密码给出密码强度判断,判断规则为:宇母数字符号只有一种则为弱,字
母数字符号任意包含两种则为中,字母数字符号包含三种则为强。
HTML:
<template> <el-form ref="form" :model="user" :rules="rules" label-width="80px"> <el-form-item label="旧密码" prop="oldPassword"> <el-input v-model="user.oldPassword" placeholder="请输入旧密码" type="password" show-password /> </el-form-item> <el-form-item label="新密码" prop="newPassword"> <el-input v-model="user.newPassword" placeholder="请输入新密码" type="password" show-password @input="checkPasswordStrength" /> <span >密码强度:<span :class="tipsColor">{{ strength }}</span></span > </el-form-item> <el-form-item label="确认密码" prop="confirmPassword"> <el-input v-model="user.confirmPassword" placeholder="请确认新密码" type="password" show-password /> </el-form-item> <el-form-item> <el-button type="primary" size="mini" @click="submit">保存</el-button> <el-button type="danger" size="mini" @click="close">关闭</el-button> </el-form-item> </el-form> </template> <script> import { updateUserPwd } from "@/api/system/user"; export default { data() { const equalToPassword = (rule, value, callback) => { if (this.user.newPassword !== value) { callback(new Error("两次输入的密码不一致")); } else { callback(); } }; return { strength: "", user: { oldPassword: undefined, newPassword: undefined, confirmPassword: undefined, }, // 表单校验 rules: { oldPassword: [ { required: true, message: "旧密码不能为空", trigger: "blur" }, ], newPassword: [ { required: true, message: "新密码不能为空", trigger: "blur" }, { min: 6, max: 18, message: "长度在 6 到 18 个字符", trigger: "blur", }, ], confirmPassword: [ { required: true, message: "确认密码不能为空", trigger: "blur" }, { required: true, validator: equalToPassword, trigger: "blur" }, ], }, }; }, computed: { // 计算两个周期值 tipsColor: function () { let str = this.strength; if(str == "弱"){ return 'color-ruo' }else if(str == "中"){ return 'color-zhong' }else if(str == "强"){ return 'color-qiang' }else{ return '' } }, }, methods: { submit() { this.$refs["form"].validate((valid) => { if (valid) { updateUserPwd(this.user.oldPassword, this.user.newPassword).then( (response) => { this.$modal.msgSuccess("修改成功"); } ); } }); }, close() { this.$tab.closePage(); }, // 对新密码进行校验 checkPasswordStrength() { let hasLetter = false; let hasNumber = false; let hasSymbol = false; for (let char of this.user.newPassword) { if (/[a-zA-Z]/.test(char)) { hasLetter = true; } else if (/\d/.test(char)) { hasNumber = true; } else { hasSymbol = true; } } if (hasLetter + hasNumber + hasSymbol === 1) { this.strength = "弱"; } else if (hasLetter + hasNumber + hasSymbol === 2) { this.strength = "中"; } else if (hasLetter + hasNumber + hasSymbol >= 3) { this.strength = "强"; } else { this.strength = ""; } }, }, }; </script> <style scoped> .color-ruo{ color: #f56c6c; } .color-zhong{ color: #e6a23c; } .color-qiang{ color: #67c23a; } </style>
效果图:
标签:vue,color,校验,else,密码,添加,user,true,strength From: https://www.cnblogs.com/houBlogs/p/17994785