微信小程序表單驗證功能完整實例
更新時間:2017年12月01日 08:53:16 作者:xiaochun365
這篇文章主要介紹了微信小程序表單驗證功能,結(jié)合完整實例形式分析了微信小程序完成表單驗證功能所涉及的視圖與邏輯操作技巧,需要的朋友可以參考下
本文實例講述了微信小程序表單驗證功能。分享給大家供大家參考,具體如下:
Wxml
<form bindsubmit="formSubmit" bindreset="formReset">
<input name="name" class="{{whoClass=='name'?'placeholderClass':'inputClass'}}" placeholder="請?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="請?zhí)顚懩氖謾C(jī)號" 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()
//表單驗證初始化
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: '請?zhí)顚懩男彰彰?,
},
mobile: {
required: '請?zhí)顚懩氖謾C(jī)號',
},
company: {
required: '請輸入公司名稱',
},
client: {
required: '請輸入綁定客戶',
}
}
)
},
//表單提交
formSubmit: function (e) {
//提交錯誤描述
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 文件點擊此處本站下載。
希望本文所述對大家微信小程序開發(fā)有所幫助。
相關(guān)文章
js中字符替換函數(shù)String.replace()使用技巧
js中字符替換函數(shù)String.replace()使用技巧,字符替換經(jīng)常用的到。2011-08-08
es6中的解構(gòu)賦值、擴(kuò)展運算符和rest參數(shù)使用詳解
這篇文章分別給大家介紹了關(guān)于es6中解構(gòu)賦值、擴(kuò)展運算符和rest參數(shù)使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09

