vue 设置密码添加弱、中、强的校验

需求:

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>

效果图:

 

posted @ 2024-01-29 16:16  _houjie  阅读(1890)  评论(0)    收藏  举报