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

Solution to MySQL IFNULL judgment problem

Problem: The null type data returned by mybatis d...

Understand the principles of MySQL persistence and rollback in one article

Table of contents redo log Why do we need to upda...

New usage of watch and watchEffect in Vue 3

Table of contents 1. New usage of watch 1.1. Watc...

Extract specific file paths in folders based on Linux commands

Recently, there is a need to automatically search...

Use node-media-server to build a simple streaming media server

Record some of the processes of using node-media-...

Analyze the selection problem of storing time and date types in MySQL

In general applications, we use timestamp, dateti...

Several ways to hide Html elements

1. Use CSS Copy code The code is as follows: style...

MySQL learning notes: data engine

View the engines supported by the current databas...

Vue implements image drag and drop function

This article example shares the specific code of ...

CentOS 6 Compile and install ZLMediaKit analysis

Install ZLMediaKit on centos6 The author of ZLMed...