Example of complete applet form verification function and example of Applet Form Verification
This example describes the form verification function of the applet. We will share this with you for your reference. The details are as follows:
Wxml
<Form bindsubmit = "formSubmit" bindreset = "formReset"> <input name = "name" class = "{whoClass = 'name '? 'Placeholderclass ': 'inputclass'} "placeholder =" Please fill in your name "type =" text "confirm-type =" next "focus =" {whoFocus = 'name '? True: false }} "bindblur =" nameBlurFocus "/> <radio-group name =" gender "bindchange =" radioChange "> <radio value =" 0 "checked/> Ms. <radio value = "1"/> SIR </radio-group> <input name = "mobile" class = "{whoClass = 'mobile '? 'Placeholderclass ': 'inputclass'} "type =" number "maxlength =" 11 "placeholder =" Enter your mobile phone number "bindblur =" mobileBlurFocus "focus =" {whoFocus = 'mobile '? True: false }}"/> <input name = "company" class = "{whoClass = 'company '? 'Placeholderclass': 'inputclass'} "placeholder =" company name "type =" text "confirm-type =" next "focus =" {whoFocus = 'company '? True: false }}"/> <input name = "client" class = "{whoClass = 'client '? 'Placeholderclass': 'inputclass'} "placeholder =" binding customer "type =" text "confirm-type =" done "focus =" {whoFocus = 'client '? True: false }}"/> <button formType = "submit"> submit </button> </form> <loading hidden = "{submitHidden}"> submitting... </loading>
App. js
import wxValidate from 'utils/wxValidate'App({ wxValidate: (rules, messages) => new wxValidate(rules, messages)})
News. js
Var appInstance = getApp () // Form Verification initialization 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: 'Enter your name',}, mobile: {required: 'enter your mobile phone number',}, company :{ Required: 'Enter the company name',}, client: {required: 'Enter the bound customer',})}, // form submission formSubmit: function (e) {// submit the error description 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 // submit 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 (){}})}
Click here for the WxValidate. js FileDownload from this site.
I hope this article will help you develop small programs.