【原创】Vue Validate 校验密码与确认密码

  methods: {
        // 校验是否阅读条款
        checkValidator(rule, value, callback) {
            if (!value) {
                this.notRead = true;
            } else {
                this.notRead = false;
                callback();
            }
        },
        // 校验密码 ==> 6-16位字母数字的组合
        passwordValidator(rule, value, callback) {
            if (value.length < 6 || value.length > 16) {
                return callback(new Error("密码必须在6到16位之间"));
            }
            let reg = new RegExp(/^(?![^a-zA-Z]+$)(?!\D+$)/);
            if (!reg.test(value)) {
                return callback(new Error("密码必须包含数字和字母"));
            }
            callback();
        },
        // 校验确认密码
        confirmPasswordValidator(rule, value, callback) {
            if (value !== this.regForm.password) {
                callback(new Error('两次密码输入不一致'));
            }
            callback();
        },
        handleRegister: function() {
            this.$refs['regForm'].validate(valid => {
                if (!valid) {
                    return;
                }
                enterPrisebasic(this.form).then(response => {
                    this.$modal.msgSuccess("注册成功")
                    this.$router.push({path:"/position"})
                });
            });
        },
        checkReadField() {
            if (!this.regForm.check) {
                this.notRead = true;
                return;
            } else {
                this.notRead = false;
            }
        },
        // 获取手机验证码
        getPhoneCode() {
            if (!/^1[34578]\d{9}$/.test(this.regForm.phoneNumber)) {
                this.$message({
                message: '请输入正确的手机号',
                type: 'warning'
                });
            }
            else {
                //1000毫秒后执行
                sendSmsCode(this.regForm.phoneNumber, 0).then(res => {
                    this.isShow = false;
                    this.disabled = true;
                    this.count = 60; //赋值3秒
                    var times = setInterval(() => {
                    this.count--; //递减
                    if (this.count <= 0) {
                        // <=0 变成获取按钮
                        this.isShow = true;
                        this.disabled = false;
                        clearInterval(times);
                    }
                    }, 1000);
                });
            }
        },
        // 获取图片验证码
        getImgCode() {
            // 获取图片验证码
            getCodeImg().then(res => {
                res = res.data;
                this.codeUrl = "data:image/gif;base64," + res.img;
                this.regForm.uuid = res.uuid;
            });
        },
        // 跳转
        toLogin() {
            this.$router.push({ path: "/position" });
        }
    }
    rules: {
        password: [
        { required: true, message: "请输入密码", trigger: "blur" },
        { required: true, validator: this.passwordValidator, trigger: "blur" }
        ],
        confirmPassword: [
        { required: true, message: "请输入确认密码", trigger: "blur" },
        { required: true, validator: this.confirmPasswordValidator, trigger: "blur" }
        ],
        check: [
        { validator: this.checkValidator}
        ]
    },
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注