vue實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時按鈕
本文實(shí)例為大家分享了vue實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時按鈕的具體代碼,供大家參考,具體內(nèi)容如下
1、點(diǎn)擊“發(fā)送驗(yàn)證碼”按鈕后進(jìn)行邏輯判斷:
▶️ 如果郵箱已輸入且格式正確:按鈕變?yōu)?strong>“已發(fā)送” ,此時為不可點(diǎn)擊狀態(tài) 并開始120s倒計(jì)時;
▶️ 如果郵箱未輸入或格式不正確:顯示錯誤的提示信息。
2、120s倒計(jì)時結(jié)束后按鈕變?yōu)?span style="color: #800000">“重新發(fā)送驗(yàn)證碼” 。
html:
<div v-bind:class="{ 'text_email': isActive, 'text_tip': isTip }">{{tip}}</div> //出錯提示
<div class="input">
<i class="ret_icon-email"></i>
<input
type="text"
v-model="email"
v-bind:class="{ 'input_email0' : hasError }"
v-on:click="cancelError"
:placeholder="輸入郵箱地址"
id="inputEmail"
/>
<br />
<input type="text" placeholder="輸入驗(yàn)證碼" class="input_number" />
<button class="btn_number" v-bind:class="{gray:wait_timer>0}" @click="getCode()">
<span
class="num_green"
v-show="showNum"
v-bind:class="{num:wait_timer>0}"
>{{this.wait_timer + "s "}}</span>
<span
class="span_number"
v-bind:class="{gray_span:wait_timer>0}"
>{{ getCodeText() }}</span>
</button>
<br />
</div>
js:
data() {
return {
tip: "用Email找回密碼",
isTip: false,
isActive: true,
showNum: false,
wait_timer: false,
hasError: false,
email: ""
}
},
methods: {
cancelError: function(event) {
this.hasError = false;
this.isActive = true;
this.isTip = false;
this.tip = "用Email找回密碼";
},
getCode: function() {
if (this.wait_timer > 0) {
return false;
}
if (!this.email) {
this.hasError = true;
this.isActive = false;
this.isTip = true;
this.tip = "Email不能為空";
return false;
}
if (
!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(
this.email
)
) {
this.hasError = true;
this.isActive = false;
this.isTip = true;
this.tip = "Email地址無效";
return false;
}
this.showNum = true;
this.wait_timer = 120;
var that = this;
var timer_interval = setInterval(function() {
if (that.wait_timer > 0) {
that.wait_timer--;
} else {
clearInterval(timer_interval);
}
}, 1000);
//在這里調(diào)取你獲取驗(yàn)證碼的ajax
},
getCodeText: function() {
if (this.wait_timer > 0) {
return "已發(fā)送";
}
if (this.wait_timer === 0) {
this.showNum = false;
return "重新發(fā)送驗(yàn)證碼!";
}
if (this.wait_timer === false) {
return "發(fā)送驗(yàn)證碼!";
}
},
}
css:
.ret_icon-email {
background: url(../../assets/pics/email.svg) no-repeat; //圖片為本地圖片
width: 20px;
height: 20px;
position: absolute;
top: 12px;
left: 16px;
}
.input_email0 {
border: 1px solid rgba(239, 83, 80, 1);
}
.input_number {
width: 112px;
height: 44px;
text-indent: 16px;
margin-right: 12px;
}
.btn_number {
width: 154px;
height: 44px;
border-radius: 4px;
box-sizing: border-box;
border: 1px solid rgba(76, 175, 80, 1);
line-height: 16px;
outline: none;
}
.span_number {
color: rgba(76, 175, 80, 1);
}
.btn_number.gray {
background: rgba(242, 244, 245, 1);
border: 1px solid rgba(0, 0, 0, 0.05);
}
.span_number.gray_span {
color: #9a9c9a;
}
.num_green.num {
color: rgba(76, 175, 80, 1);
}
效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue 獲取url參數(shù)、get參數(shù)返回?cái)?shù)組的操作
這篇文章主要介紹了vue 獲取url參數(shù)、get參數(shù)返回?cái)?shù)組的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue系列之requireJs中引入vue-router的方法
這篇文章主要介紹了vue系列之requireJs中引入vue-router的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
Vue實(shí)現(xiàn)DateRange選擇器的禁選功能
在基于Vue.js構(gòu)建的應(yīng)用程序中,處理日期選擇器是一個常見的需求,尤其是在涉及到日期范圍的選擇時,Vue提供了多種方式來實(shí)現(xiàn)日期選擇器的功能,并允許我們對這些組件進(jìn)行高度定制,本文將深入探討如何在Vue應(yīng)用中實(shí)現(xiàn)DateRange選擇器的禁選功能,需要的朋友可以參考下2024-10-10
vue3不同環(huán)境下實(shí)現(xiàn)配置代理
這篇文章主要介紹了vue3不同環(huán)境下實(shí)現(xiàn)配置代理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05

