首页 > 其他 > 详细

element-ui短信倒计时前进行字段验证

时间:2021-05-19 10:12:18      阅读:31      评论:0      收藏:0      [点我收藏+]

 

HTML部分:

<el-form-item label="手机号:" prop="phone">
    <el-input v-model.number="loginForm.phone" >{{loginForm.phone}}</el-input>
    <el-button class="validateCode" @click="sendMsg" type="primary" :disabled="isDisabled">{{buttonName}}</el-button>
</el-form-item>

 

 

 

data()部分:

data() {
// 验证手机号
var CheckTel = (rule, value, callback) => {
    if (!value) {
        callback(new Error(‘请输入手机号码‘));
    } else if (!Number.isInteger(value)) {
        callback(new Error(‘手机号码必须是数字‘));
    } else if (value.toString().length != 11) {
        callback(new Error(‘手机号码必须是11位‘));
    } else {
        callback();
    }
};

return {
    disabled: true,
    buttonName: "发送短信",
    isDisabled: false,
    time: 10,
    loginForm: {
        phone: ‘‘,
        validateCode: ‘‘,
    },
    rules: {
        phone: [{
            required: true,
            validator: CheckTel,
            trigger: ‘blur‘
        }],
        validateCode: [{
            required: true,
            message: ‘验证码不能为空‘,
            trigger: ‘blur‘
        }],
    },
}
},
methods部分:

sendMsg() {
    // alert(this.rules.phone);
    this.$refs.loginForm.validateField(‘phone‘,valid=>{
        if (valid!==‘‘) {
            this.rules.phone;
        }else {
        let me = this;
        me.isDisabled = true;
        let interval = window.setInterval(function () {
            me.buttonName = me.time + ‘后重新发送‘;
            --me.time;
            if (me.time < 0) {
                me.buttonName = "重新发送";
                me.time = 10;
                me.isDisabled = false;
                window.clearInterval(interval);
            }
        }, 1000);
    }
});

}

element-ui短信倒计时前进行字段验证

原文:https://www.cnblogs.com/liang715200/p/14783314.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!