微信小程序如何獲取手機(jī)驗(yàn)證碼
一種比較常見的功能獲取手機(jī)驗(yàn)證碼,供大家參考,具體內(nèi)容如下
先看效果圖:

其實(shí)這個(gè)功能實(shí)現(xiàn)起來(lái)很簡(jiǎn)單,主要就是調(diào)取第三方接口,拿到返回值驗(yàn)證的問(wèn)題
直接上代碼吧:
<view class='changeInfo'>
<view class='changeInfoName'>
<input placeholder='請(qǐng)輸入姓名' bindinput='getNameValue' value='{{name}}'/>
</view>
<view class='changeInfoName'>
<input placeholder='請(qǐng)輸入手機(jī)號(hào)' bindinput='getPhoneValue' value='{{phone}}'/>
</view>
<view class='changeInfoName'>
<input placeholder='請(qǐng)輸驗(yàn)證碼' bindinput='getCodeValue' value='{[code]}' style='width:70%;'/>
<button class='codeBtn' bindtap='getVerificationCode' disabled='{{disabled}}' >{{codename}}</button>
</view>
<button class='changeBtn' bindtap='save'>保存</button>
</view>
CSS:
page{
height: 100%;
width: 100%;
background: linear-gradient(#5681d7, #486ec3);
}
.changeInfo{
display: flex;
flex-direction: column;
justify-content: space-between;
width: 90%;
margin: 50rpx auto;
}
.changeInfoName{
position: relative;
height: 80rpx;
width: 100%;
border-radius: 10rpx;
background: #fff;
margin-bottom: 20rpx;
padding-left: 20rpx;
box-sizing: border-box;
}
.codeBtn{
position: absolute;
right: 0;
top: 0;
color: #bbb;
width: 30%;
font-size: 26rpx;
height: 80rpx;
line-height: 80rpx;
}
.changeInfoName input{
width: 100%;
height:100%;
}
.changeBtn{
width: 40%;
height: 100rpx;
background: #fff;
color: #000;
border-radius: 50rpx;
position: absolute;
bottom: 10%;
left: 50%;
margin-left: -20%;
line-height: 100rpx;
}
js:
var app = require('../../resource/js/util.js');
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
name:'',//姓名
phone:'',//手機(jī)號(hào)
code:'',//驗(yàn)證碼
iscode:null,//用于存放驗(yàn)證碼接口里獲取到的code
codename:'獲取驗(yàn)證碼'
},
//獲取input輸入框的值
getNameValue:function(e){
this.setData({
name:e.detail.value
})
},
getPhoneValue:function(e){
this.setData({
phone:e.detail.value
})
},
getCodeValue: function (e) {
this.setData({
code: e.detail.value
})
},
getCode:function(){
var a = this.data.phone;
var _this = this;
var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
if (this.data.phone == "") {
wx.showToast({
title: '手機(jī)號(hào)不能為空',
icon: 'none',
duration: 1000
})
return false;
} else if (!myreg.test(this.data.phone)) {
wx.showToast({
title: '請(qǐng)輸入正確的手機(jī)號(hào)',
icon: 'none',
duration: 1000
})
return false;
}else{
wx.request({
data: {},
'url': 接口地址,
success(res) {
console.log(res.data.data)
_this.setData({
iscode: res.data.data
})
var num = 61;
var timer = setInterval(function () {
num--;
if (num <= 0) {
clearInterval(timer);
_this.setData({
codename: '重新發(fā)送',
disabled: false
})
} else {
_this.setData({
codename: num + "s"
})
}
}, 1000)
}
})
}
},
//獲取驗(yàn)證碼
getVerificationCode() {
this.getCode();
var _this = this
_this.setData({
disabled: true
})
},
//提交表單信息
save:function(){
var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
if(this.data.name == ""){
wx.showToast({
title: '姓名不能為空',
icon: 'none',
duration: 1000
})
return false;
}
if(this.data.phone == ""){
wx.showToast({
title: '手機(jī)號(hào)不能為空',
icon: 'none',
duration: 1000
})
return false;
}else if(!myreg.test(this.data.phone)){
wx.showToast({
title: '請(qǐng)輸入正確的手機(jī)號(hào)',
icon: 'none',
duration: 1000
})
return false;
}
if(this.data.code == ""){
wx.showToast({
title: '驗(yàn)證碼不能為空',
icon: 'none',
duration: 1000
})
return false;
}else if(this.data.code != this.data.iscode){
wx.showToast({
title: '驗(yàn)證碼錯(cuò)誤',
icon: 'none',
duration: 1000
})
return false;
}else{
wx.setStorageSync('name', this.data.name);
wx.setStorageSync('phone', this.data.phone);
wx.redirectTo({
url: '../add/add',
})
}
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面加載
*/
onLoad: function (options) {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽頁(yè)面卸載
*/
onUnload: function () {
},
/**
* 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動(dòng)作
*/
onPullDownRefresh: function () {
},
/**
* 頁(yè)面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
},
/**
* 用戶點(diǎn)擊右上角分享
*/
onShareAppMessage: function () {
}
})
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 微信小程序?qū)崿F(xiàn)手機(jī)驗(yàn)證碼登錄
- 微信小程序?qū)崿F(xiàn)手機(jī)獲取驗(yàn)證碼倒計(jì)時(shí)60s
- 微信小程序獲取手機(jī)驗(yàn)證碼的方法
- 微信小程序手機(jī)號(hào)驗(yàn)證碼登錄的項(xiàng)目實(shí)現(xiàn)
- 微信小程序綁定手機(jī)號(hào)獲取驗(yàn)證碼功能
- 微信小程序?qū)崿F(xiàn)基于三元運(yùn)算驗(yàn)證手機(jī)號(hào)/姓名功能示例
- 微信小程序手機(jī)號(hào)碼驗(yàn)證功能的實(shí)例代碼
- 微信小程序 功能函數(shù)小結(jié)(手機(jī)號(hào)驗(yàn)證*、密碼驗(yàn)證*、獲取驗(yàn)證碼*)
- 微信小程序?qū)崿F(xiàn)手機(jī)號(hào)碼驗(yàn)證
相關(guān)文章
JavaScript實(shí)現(xiàn)時(shí)間格式的切割與轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了使用JavaScript實(shí)現(xiàn)時(shí)間格式的切割與轉(zhuǎn)換的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2024-04-04
js判斷數(shù)組是否包含某個(gè)字符串變量的實(shí)例
下面小編就為大家分享一篇js判斷數(shù)組是否包含某個(gè)字符串變量的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-11-11
javascript顯示動(dòng)態(tài)時(shí)間的方法匯總
本文給大家匯總介紹了3種javascript實(shí)現(xiàn)動(dòng)態(tài)顯示時(shí)間的方法及詳細(xì)示例,有需要的小伙伴可以參考下2018-07-07
JS 網(wǎng)頁(yè)彩蛋 實(shí)現(xiàn)代碼
顯示一定時(shí)間后消失,刷新之后清空變量值,重新開始記錄鍵值。 程序的原理就是檢測(cè)按鍵的鍵值,當(dāng)達(dá)到預(yù)定條件時(shí)執(zhí)行規(guī)定的函數(shù)。2009-09-09
利用Query+bootstrap和js兩種方式實(shí)現(xiàn)日期選擇器
日期選擇器在我們平時(shí)開發(fā)的時(shí)候經(jīng)常要用到,下面這篇文章主要給大家介紹了利用Query+bootstrap和js這兩種方式實(shí)現(xiàn)日期選擇器的方法,文中兩種方法都給出了詳細(xì)的示例代碼,有需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-01-01
Javascript實(shí)現(xiàn)商品秒殺倒計(jì)時(shí)(時(shí)間與服務(wù)器時(shí)間同步)
在一些購(gòu)物商城經(jīng)??吹接泻芏嗌唐纷雒霘⒒顒?dòng),也就是倒計(jì)時(shí),本篇文章給大家介紹Javascript實(shí)現(xiàn)商品秒殺倒計(jì)時(shí)(時(shí)間與服務(wù)器時(shí)間同步),需要的朋友可以了解下2015-09-09
JS實(shí)現(xiàn)合并兩個(gè)數(shù)組并去除重復(fù)項(xiàng)只留一個(gè)的方法
這篇文章主要介紹了JS實(shí)現(xiàn)合并兩個(gè)數(shù)組并去除重復(fù)項(xiàng)只留一個(gè)的方法,涉及JavaScript數(shù)組合并及去重的相關(guān)技巧,需要的朋友可以參考下2015-12-12

