js获取验证码
最近用到验证码,记录一下,免得自己忘记,下次直接用
<el-input
v-model="input2"
placeholder="请输入验证码"
v-on:input="setReg()"//监听每一次input的输入
>
<span slot="prepend">验证码 :</span>
<span slot="append">
<span class="yzm" v-if="codeShow" @click="getcode">获取验证码</span>
<span class="count" v-if="!codeShow">{{ count }}秒后重试</span>
</span>
</el-input>
data() {
return {
input2: "",
codeShow: true,
count: "",
timer: null,
};
},
method:{
async getcode() {
var Reg = this.Reg;
if (Reg.test(this.input1)) {
const TIME_COUNT = 30;
if (!this.timer) {
this.count = TIME_COUNT;
this.codeShow = false;
this.timer = setInterval(() => {
if (this.count > 0 && this.count <= TIME_COUNT) {
this.count--;
} else {
this.codeShow = true;
clearInterval(this.timer);
this.timer = null;
}
}, 1000);
}
this.getPhonVer();//校验方法
} else {
show_msg(this.msg.inputname1 + "格式不正确");
// this.$message.error(this.msg.inputname1 + "格式不正确");
}
},
}