I personally feel that the development framework of WeChat applet is generally similar to VUE, but its form component does not have its own validation function. Therefore, there are generally two methods when developing form validation for applet. One is to write the validation rules yourself, but it requires a solid foundation in regular expressions. The other is to use the WxValidate plug-in developed by the official community for form validation.
First of all, the download address and official documentation of the plug-in are in WxValidate download address and document address The specific location of the WxValidate.js file is wx-extend/src/assets/plugins/ wx-validate /WxValidate.js The first method to introduce is to copy the plug-in file to the file directory you need After that, you can use local reference to introduce the plug-in into the JS file of the page you need. The specific operations are as follows //In the index.js page, import WxValidate from '../../utils/WxValidate.js' const app = getApp() Page({ data: { form: { name: '', phone: '' } } })
Then pay attention to the data binding of the form component in the wxml file, otherwise the rules cannot be verified no matter how the form component is filled in. The binding method of the form component is as follows //wxml page <form bindsubmit="formSubmit"> <view class="weui-cells__title">Please fill in your personal information</view> <view class="weui-cells weui-cells_after-title"> <view class="weui-cell weui-cell_input"> <view class="weui-cell__hd"> <view class="weui-label">Name</view> </view> <view class="weui-cell__bd"> <input class="weui-input" name='name' value='{{form.name}}' placeholder="Please enter your name" /> </view> </view> <view class="weui-cell weui-cell_input weui-cell_vcode"> <view class="weui-cell__hd"> <view class="weui-label">Mobile phone number</view> </view> <view class="weui-cell__bd"> <input class="weui-input" name='phone' type='number' value='{{form.phone}}' placeholder="Please enter your phone number" /> </view> </view> </view> </form>
Then add the form binding to the js file //index.js Page({ data: { form: { name: '', phone: '' } } }) Then comes the most important verification rule writing First, you need to add the validation rule function to the onLoad function // There are multiple functions in onLoad. Write the function name in the onLoad function and define the function outside onLoad onLoad() { this.getuser() this.initValidate()//Validation rule function} //OnLoad has only one function in it onLoad:function(){ rules:{} messages:{} }
Then there is the code for validation rules and error rules // Report an error showModal(error) { wx.showModal({ content: error.msg, showCancel: false, }) }, //Verification function initValidate() { const rules = { name: { required: true, minlength:2 }, phone:{ required:true, tel:true } } const messages = { name: { required: 'Please fill in your name', minlength:'Please enter a correct name' }, phone:{ required:'Please fill in your mobile number', Tel:'Please fill in the correct mobile phone number' } } this.WxValidate = new WxValidate(rules, messages) }, //Call verification function formSubmit: function(e) { console.log('Submit event occurred in the form, the data carried is:', e.detail.value) const params = e.detail.value //Check form if (!this.WxValidate.checkForm(params)) { const error = this.WxValidate.errorList[0] this.showModal(error) return false } this.showModal({ msg: 'Submission successful' }) }
Let's take a look at the demonstration effect
This is the end of this article about the use of WxValidate for form validation in WeChat mini-program development. For more relevant mini-program form validation content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: The phenomenon of margin-top collapse and the specific solution
>>: How to publish static resources in nginx
This article shares the specific code for JavaScr...
First, install PHP5 very simple yum install php T...
Table of contents Controller type of k8s Relation...
Volume Label, Property Name, Description 002 <...
In the process of web project development, we oft...
1. Spread Operator The spread operator is three d...
Table of contents 1. Optional chaining operator [...
Table of contents 1. Project Description 1.1 Back...
Table of contents 1. Enter the network card confi...
1. Install tomcat8 with docker 1. Find the tomcat...
Mirroring is also one of the core components of D...
1. Download nginx [root@localhost my.Shells]# doc...
Table of contents 1. Browser support 2. export ex...
1. Complexity of front-end engineering If we are ...
If we want to perform batch operations on a type ...