uniapp?手機(jī)驗(yàn)證碼輸入框?qū)崿F(xiàn)代碼(隨機(jī)數(shù)、倒計(jì)時(shí)、隱藏手機(jī)號(hào)碼中間四位)可以直接使用





如鍵盤(pán)被隱藏,可直接點(diǎn)擊藍(lán)框彈出鍵盤(pán),藍(lán)框就相當(dāng)于input的光標(biāo),驗(yàn)證碼輸入錯(cuò)誤之后會(huì)將字體以及邊框改為紅色,持續(xù)1.5s(可自行修改時(shí)間),然后清空數(shù)據(jù)。
<template>
<view class="code">
<view class="code-tip-one">請(qǐng)輸入驗(yàn)證碼
<view class="code-tip">已向<text>+86 {{phone.substring(0, 3)}}****{{phone.substr(phone.length-4)}}</text>發(fā)送驗(yàn)證碼</view>
<view class="code-errow" v-if="codeclolor == '#ff0000'">驗(yàn)證碼輸入錯(cuò)誤</view>
</view>
<input class="cinput" adjust-position="false" auto-blur="true" @blur="blur" @input="codenum" :focus="focus"
value="code" v-model="code" type="number" maxlength="6" />
<view class="code-input">
<view v-for="(item,index) in 6" :key="index" @click="codefocus(index)"
:style='(index == code.length? "border: 5rpx solid #1195db;width: 80rpx;height: 80rpx;line-height: 80rpx;":"color: " + codeclolor + ";" +"border: 2rpx solid" + codeclolor)'>
{{code[index] && code[index] || ''}}
</view>
</view>
<block v-if="sec!=20">
<view class="recode">重新發(fā)送({{sec}}s)</view>
</block>
<button @click="getCode()" type="primary" :disabled="verifyShow" style="margin-top: 200rpx;">發(fā)送短信</button>
</view>
</template>
<script>
export default {
data() {
return {
phone:'12345678910',
// 驗(yàn)證碼輸入聚焦
focus: true,//input焦點(diǎn),用于鍵盤(pán)隱藏后重新喚起
// 驗(yàn)證碼框顏色
codeclolor: "#313131",//自定義光標(biāo)的顏色
// 驗(yàn)證碼獲取秒數(shù)
sec: '20',//這是重新獲取驗(yàn)證碼的倒計(jì)時(shí)(可根據(jù)需求修改)
code: '',//這是用戶(hù)輸入的驗(yàn)證碼
codeCorrect:'',//正確的驗(yàn)證碼
verifyShow:false,//是否禁用按鈕
}
},
methods: {
// 輸入驗(yàn)證碼
codenum: function(event) {
// console.log('輸入的值',event.target.value)
var that = this
var code = event.target.value
that.code = code
if (code.length == 6) {
if (code == that.codeCorrect) {
//輸入六位驗(yàn)證碼后自動(dòng)進(jìn)行驗(yàn)證并執(zhí)行驗(yàn)證成功的函數(shù)
console.log('驗(yàn)證碼正確:',that.code)
} else {
console.log('驗(yàn)證碼錯(cuò)誤!!!:',that.code)
that.codeclolor = "#ff0000"
setTimeout(function() {
that.code = []
event.target.value = ""
that.codeclolor = "#313131"
}, 1500)
}
}
},
// 鍵盤(pán)隱藏后設(shè)置失去焦點(diǎn)
blur: function() {
var that = this
that.focus = false
},
// 點(diǎn)擊自定義光標(biāo)顯示鍵盤(pán)
codefocus: function(e) {
var that = this
if (e == that.code.length) {
that.focus = true
}
},
getCode(){//獲取驗(yàn)證碼
const that = this
that.codeCorrect = that.getVerificationCode(6) //可以不傳值,默認(rèn)為4位隨機(jī)碼
console.log('生成的隨機(jī)碼為:' + that.codeCorrect)
that.timedown(that.sec)// 倒計(jì)時(shí)
},
//隨機(jī)生成幾位數(shù)
getVerificationCode(codeLength){ //傳入需要的字符串長(zhǎng)度,不傳默認(rèn)為4
// 準(zhǔn)備一個(gè)用來(lái)抽取碼的字符串,或者字典
// let verification_code_str = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; //數(shù)字和字母
let verification_code_str = "0123456789"; //純數(shù)字
// 獲取某個(gè)范圍的隨機(jī)整數(shù),封裝的函數(shù),在上面抽取字典的時(shí)候進(jìn)行了調(diào)用
function getRandom(min, max) {//意思是獲取min-max數(shù)字之間的某個(gè)隨機(jī)數(shù),直接調(diào)用即可
return Math.round(Math.random() * (max - min) + min);
}
let newStr = ''; //創(chuàng)建一個(gè)空字符串,用來(lái)拼接四位隨機(jī)碼
for (var i = 0; i < codeLength; i++) { //for循環(huán)四次,則拼接四次隨機(jī)碼
newStr += verification_code_str[getRandom(0, verification_code_str.length - 1)]; //從字典中隨機(jī)選一個(gè)下標(biāo),并拼接到空字符串中
}
return newStr
},
//倒計(jì)時(shí)
timedown:function(num){
let that = this;
if(num == 0){
that.verifyShow = false; // 不禁用獲取驗(yàn)證碼按鈕
that.sec = 20
return clearTimeout();
}else{
that.verifyShow = true; // 禁用獲取驗(yàn)證碼按鈕
setTimeout(function() {
that.sec = num-1
that.timedown(num-1);
}, 1000);//定時(shí)每秒減一
}
},
}
}
</script>
<style scoped lang="less">
.code {
margin: auto;
margin-top: 50rpx;
width: 650rpx;
height: auto;
}
.code-tip-one {
width: 650rpx;
height: 250rpx;
line-height: 100rpx;
font-size: 60rpx;
font-weight: bold;
color: #313131;
}
.code-tip {
width: 650rpx;
height: 100rpx;
line-height: 50rpx;
font-size: 30rpx;
font-weight: normal;
color: #8a8a8a;
}
.code-errow {
width: 650rpx;
height: 50rpx;
line-height: 25rpx;
font-size: 28rpx;
font-weight: normal;
color: #ff0000;
}
.code-tip>text {
padding: 0 20rpx;
width: 650rpx;
font-size: 30rpx;
font-weight: normal;
color: #ff5500;
}
.code-input {
margin: auto;
width: 650rpx;
height: 100rpx;
display: flex;
}
.code-input>view {
margin-top: 5rpx;
margin-left: 15rpx;
width: 86rpx;
height: 86rpx;
line-height: 86rpx;
font-size: 60rpx;
font-weight: bold;
color: #313131;
text-align: center;
border-radius: 10rpx;
}
.code-input>view:nth-child(1) {
margin-left: 0rpx;
}
.cinput {
position: fixed;
left: -100rpx;
width: 50rpx;
height: 50rpx;
}
.recode{
margin-top: 20rpx;
width: 200rpx;
height: 80rpx;
line-height: 80rpx;
color: #707070;
font-size: 28rpx;
}
</style>
實(shí)現(xiàn)思路:創(chuàng)建六個(gè)正方形的view(使用for循環(huán)),然后創(chuàng)建一個(gè)數(shù)字input,最大輸入長(zhǎng)度為六位(根據(jù)驗(yàn)證碼的長(zhǎng)度),再將input隱藏掉,獲取到的值分別放到六個(gè)view中。
其中驗(yàn)證碼驗(yàn)證失敗之后利用v-model雙向綁定進(jìn)行清空已經(jīng)輸入的值
注意:?jiǎn)渭兊妮敵?nbsp;code[index] 不會(huì)展示空只會(huì)展示未定義,必須加上 {{code[index] && code[index] || ''}} 進(jìn)行判斷替換為空,密碼輸入框替換字符,也就是與或非的意思吧
如果是不想展示驗(yàn)證碼信息可以改為{{code[index] && '●' || ''}},這樣你輸入是參數(shù)就會(huì)被替換為●●●●●●
到此這篇關(guān)于uniapp 手機(jī)驗(yàn)證碼輸入框(隨機(jī)數(shù)、倒計(jì)時(shí)、隱藏手機(jī)號(hào)碼中間四位)可以直接使用的文章就介紹到這了,更多相關(guān)uniapp驗(yàn)證碼輸入框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
手寫(xiě)TypeScript?時(shí)很多人常犯的幾個(gè)錯(cuò)誤
這篇文章主要介紹了手寫(xiě)TypeScript?時(shí)很多人常犯的幾個(gè)錯(cuò)誤,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的抽卡,重要的朋友可以參考一下2022-09-09
uniapp web-view組件雙向通信的問(wèn)題記錄
本文主要介紹在uniapp中頁(yè)面與webview組件內(nèi)頁(yè)面的雙向通信問(wèn)題,本文通過(guò)圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-07-07
微信小程序使用request網(wǎng)絡(luò)請(qǐng)求操作實(shí)例
這篇文章主要介紹了微信小程序使用request網(wǎng)絡(luò)請(qǐng)求操作,結(jié)合實(shí)例形式分析了wx.request(object)網(wǎng)絡(luò)請(qǐng)求操作的具體使用技巧,需要的朋友可以參考下2017-12-12
JavaScript實(shí)現(xiàn)使用Canvas繪制圖形的基本教程
本篇文章主要介紹了JavaScript實(shí)現(xiàn)使用Canvas繪制圖形的基本教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10
微信小程序?qū)崿F(xiàn)客服功能(客服消息)的全過(guò)程
JavaScript實(shí)現(xiàn)仿網(wǎng)易通行證表單驗(yàn)證
js實(shí)現(xiàn)表單檢測(cè)及表單提示的方法

