微信小程序表單驗(yàn)證功能完整實(shí)例
更新時(shí)間:2017年12月01日 08:53:16 作者:xiaochun365
這篇文章主要介紹了微信小程序表單驗(yàn)證功能,結(jié)合完整實(shí)例形式分析了微信小程序完成表單驗(yàn)證功能所涉及的視圖與邏輯操作技巧,需要的朋友可以參考下
本文實(shí)例講述了微信小程序表單驗(yàn)證功能。分享給大家供大家參考,具體如下:
Wxml
<form bindsubmit="formSubmit" bindreset="formReset"> <input name="name" class="{{whoClass=='name'?'placeholderClass':'inputClass'}}" placeholder="請(qǐng)?zhí)顚懩男彰? type="text" confirm-type="next" focus="{{whoFocus=='name'?true:false}}" bindblur="nameBlurFocus" /> <radio-group name="gender" bindchange="radioChange"> <radio value="0" checked />女士 <radio value="1" />先生 </radio-group> <input name="mobile" class="{{whoClass=='mobile'?'placeholderClass':'inputClass'}}" type="number" maxlength="11" placeholder="請(qǐng)?zhí)顚懩氖謾C(jī)號(hào)" bindblur="mobileBlurFocus" focus="{{whoFocus=='mobile'?true:false}}" /> <input name="company" class="{{whoClass=='company'?'placeholderClass':'inputClass'}}" placeholder="公司名稱" type="text" confirm-type="next" focus="{{whoFocus=='company'?true:false}}" /> <input name="client" class="{{whoClass=='client'?'placeholderClass':'inputClass'}}" placeholder="綁定客戶" type="text" confirm-type="done" focus="{{whoFocus=='client'?true:false}}" /> <button formType="submit">提交</button> </form> <loading hidden="{{submitHidden}}"> 正在提交... </loading>
app.js
import wxValidate from 'utils/wxValidate' App({ wxValidate: (rules, messages) => new wxValidate(rules, messages) })
news.js
var appInstance = getApp() //表單驗(yàn)證初始化 onLoad: function () { this.WxValidate = appInstance.WxValidate( { name: { required: true, minlength: 2, maxlength: 10, }, mobile: { required: true, tel: true, }, company: { required: true, minlength: 2, maxlength: 100, }, client: { required: true, minlength: 2, maxlength: 100, } } , { name: { required: '請(qǐng)?zhí)顚懩男彰彰?, }, mobile: { required: '請(qǐng)?zhí)顚懩氖謾C(jī)號(hào)', }, company: { required: '請(qǐng)輸入公司名稱', }, client: { required: '請(qǐng)輸入綁定客戶', } } ) }, //表單提交 formSubmit: function (e) { //提交錯(cuò)誤描述 if (!this.WxValidate.checkForm(e)) { const error = this.WxValidate.errorList[0] // `${error.param} : ${error.msg} ` wx.showToast({ title: `${error.msg} `, image: '/pages/images/error.png', duration: 2000 }) return false } this.setData({ submitHidden: false }) var that = this //提交 wx.request({ url: '', data: { Realname: e.detail.value.name, Gender: e.detail.value.gender, Mobile: e.detail.value.mobile, Company: e.detail.value.company, client: e.detail.value.client, Identity: appInstance.userState.identity }, method: 'POST', success: function (requestRes) { that.setData({ submitHidden: true }) appInstance.userState.status = 0 wx.navigateBack({ delta: 1 }) }, fail: function () { }, complete: function () { } }) }
WxValidate.js 文件點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家微信小程序開發(fā)有所幫助。
相關(guān)文章
js判斷空對(duì)象的實(shí)例(超簡(jiǎn)單)
下面小編就為大家?guī)硪黄猨s判斷空對(duì)象的實(shí)例(超簡(jiǎn)單)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07js中字符替換函數(shù)String.replace()使用技巧
js中字符替換函數(shù)String.replace()使用技巧,字符替換經(jīng)常用的到。2011-08-08hammer.js實(shí)現(xiàn)圖片手勢(shì)放大效果
這篇文章主要為大家詳細(xì)介紹了hammer.js實(shí)現(xiàn)圖片手勢(shì)放大效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08es6中的解構(gòu)賦值、擴(kuò)展運(yùn)算符和rest參數(shù)使用詳解
這篇文章分別給大家介紹了關(guān)于es6中解構(gòu)賦值、擴(kuò)展運(yùn)算符和rest參數(shù)使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09