WeChat applet implements form verification

WeChat applet implements form verification

WeChat applet form validation, for your reference, the specific content is as follows

You need to use a plug-in WxValidat.js

Portal

Import it under the page js file you need to use

import WxValidate from '../../utils/WxValidate.js'

Main content

WXML Content

<form bindsubmit="formSubmit">
  <view class="weui-cells__title">Username</view>
<view class="weui-cells weui-cells_after-title">
  <view class="weui-cell weui-cell_input">
   <input class="weui-input" type="text" name="userName" placeholder="Please enter your user name"/>
  </view>
</view>
  <view class="weui-cells__title">Password</view>
<view class="weui-cells weui-cells_after-title">
  <view class="weui-cell weui-cell_input">
   <input class="weui-input" type="text" name="password" placeholder="Please enter your password"/>
  </view>
</view>
  <view class="weui-cells__title">Mobile phone number</view>
<view class="weui-cells weui-cells_after-title">
  <view class="weui-cell weui-cell_input">
   <input class="weui-input" type="text" name="phone" placeholder="Please enter your phone number"/>
  </view>
</view>
  <view class="btn-area">
    <button formType="submit">Submit</button>
    <button formType="reset">Reset</button>
  </view>
</form>

js content

 /**
   * Life cycle function--listen for page loading*/
 onLoad: function (options) {
     this.initValidate()//Validation rule function, initialize field rules and field prompt information},
       
   initValidate() {
     const rules = {
       userName: { //Username required: true,  
         minlength:2 
       },
       password: { //password required: true
       },
       phone:{ //Mobile numberrequired:true,
         tel:true
       }
     }
     const messages = { //prompt information userName: {
         required: 'Please fill in your name',
         minlength:'Please enter a correct name'
       },
       password: {
         required: 'Please fill in your password'
       },
       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]
       console.log(error);
       return false
     }
     console.log("yes");
     return true;
},

It is worth noting that the errorList list of WxValidate returns an object.

And the name of the field to be verified should correspond one to one with the name of the form component.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to change the dot in the WeChat applet swiper-dot into a slider
  • WeChat applet 12 lines of js code to write a slider function yourself (recommended)
  • WeChat applet slider verification method

<<:  Detailed explanation of the meaning of N and M in the MySQL data type DECIMAL(N,M)

>>:  VMware Workstation 12 Pro Linux installation tutorial

Recommend

Dynamic starry sky background implemented with CSS3

Result:Implementation Code html <link href=...

How to dynamically modify container port mapping in Docker

Preface: Docker port mapping is often done by map...

The iframe refresh method is more convenient

How to refresh iframe 1. To refresh, you can use j...

HTML Tutorial: Collection of commonly used HTML tags (4)

Related articles: Beginners learn some HTML tags ...

How Web Designers Create Images for Retina Display Devices

Special statement: This article is translated bas...

Combining XML and CSS styles

student.xml <?xml version="1.0" enco...

Analysis of Linux configuration to achieve key-free login process

1.ssh command In Linux, you can log in to another...

WeChat applet calculator example

This article shares the specific code of the WeCh...

Vue+el-table realizes merging cells

This article example shares the specific code of ...

How to use partitioning to optimize MySQL data processing for billions of data

When MySQL queries tens of millions of data, most...

How to Set Shortcut Icons in Linux

Preface Creating shortcuts in Linux can open appl...

Rendering Function & JSX Details

Table of contents 1. Basics 2. Nodes, trees, and ...

How to automatically backup mysql remotely under Linux

Preface: Basically, whether it is for our own use...