溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

微信小程序驗證碼的實現(xiàn)方法

發(fā)布時間:2020-12-21 11:31:52 來源:億速云 閱讀:202 作者:小新 欄目:移動開發(fā)

這篇文章給大家分享的是有關(guān)微信小程序驗證碼的實現(xiàn)方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。

驗證碼:

<input bindinput="makecodeInput" type="text">
<view bindtap="getcode" class="makecode">{{code}}</view><button bindtap="login">登錄</button>

index.js

data: {
code: "",
makecode:"",
},
//獲取輸入驗證碼
makecodeInput:function(e){
this.setData({
makecode:e.detail.value
})
},
// 登錄
login: function() {
if(this.data.makecode != this.data.code){
wx.showToast({
title: '驗證碼不正確',
icon: 'none',
duration: 2000
})
}
},
//驗證碼
createCode() {
var code;
//首先默認code為空字符串
code = '';
//設(shè)置長度,這里看需求,我這里設(shè)置了4
var codeLength = 4;
//設(shè)置隨機字符
var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
//循環(huán)codeLength 我設(shè)置的4就是循環(huán)4次
for (var i = 0; i < codeLength; i++) {
//設(shè)置隨機數(shù)范圍,這設(shè)置為0 ~ 10
var index = Math.floor(Math.random() * 10);
//字符串拼接 將每次隨機的字符 進行拼接
code += random[index];
}
//將拼接好的字符串賦值給展示的code
this.setData({
code: code
})
},
/**
* 生命周期函數(shù)--監(jiān)聽頁面加載
*/
onLoad: function(options) {
this.createCode();
},
getcode: function() {
this.createCode();
},

感謝各位的閱讀!關(guān)于微信小程序驗證碼的實現(xiàn)方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI