您好,登錄后才能下訂單哦!
小編給大家分享一下微信小程序表單驗證功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
Wxml
<form bindsubmit="formSubmit" bindreset="formReset"> <input name="name" class="{{whoClass=='name'?'placeholderClass':'inputClass'}}" placeholder="請填寫您的姓名" 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="請填寫您的手機號" 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: '請填寫您的姓名姓名', }, mobile: { required: '請填寫您的手機號', }, 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 () { } }) }
以上是“微信小程序表單驗證功能”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。