Complete example of WeChat mini program form verification function

小云云
Release: 2018-05-16 09:58:01
Original
6690 people have browsed it

Combined with the complete example form, the view and logical operation skills involved in completing the form verification function of the WeChat applet are analyzed. This article mainly introduces the form verification function of the WeChat applet. Friends in need can refer to it.

The example in this article describes the form verification function of the WeChat applet. Share it with everyone for your reference, the details are as follows:

Wxml

<form bindsubmit="formSubmit" bindreset="formReset">
 <input name="name" class="{{whoClass==&#39;name&#39;?&#39;placeholderClass&#39;:&#39;inputClass&#39;}}" placeholder="请填写您的姓名" type="text" confirm-type="next" focus="{{whoFocus==&#39;name&#39;?true:false}}" bindblur="nameBlurFocus" />
 <radio-group name="gender" bindchange="radioChange">
  <radio value="0" checked />女士
  <radio value="1" />先生
 </radio-group>
 <input name="mobile" class="{{whoClass==&#39;mobile&#39;?&#39;placeholderClass&#39;:&#39;inputClass&#39;}}" type="number" maxlength="11" placeholder="请填写您的手机号" bindblur="mobileBlurFocus" focus="{{whoFocus==&#39;mobile&#39;?true:false}}" />
 <input name="company" class="{{whoClass==&#39;company&#39;?&#39;placeholderClass&#39;:&#39;inputClass&#39;}}" placeholder="公司名称" type="text" confirm-type="next" focus="{{whoFocus==&#39;company&#39;?true:false}}" />
 <input name="client" class="{{whoClass==&#39;client&#39;?&#39;placeholderClass&#39;:&#39;inputClass&#39;}}" placeholder="绑定客户" type="text" confirm-type="done" focus="{{whoFocus==&#39;client&#39;?true:false}}" />
 <button formType="submit">提交</button>
</form>
<loading hidden="{{submitHidden}}">
 正在提交...
</loading>
Copy after login

app.js

import wxValidate from &#39;utils/wxValidate&#39;
App({
  wxValidate: (rules, messages) => new wxValidate(rules, messages)
})
Copy after login

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: &#39;请填写您的姓名姓名&#39;,
        },
        mobile: {
          required: &#39;请填写您的手机号&#39;,
        },
        company: {
          required: &#39;请输入公司名称&#39;,
        },
        client: {
          required: &#39;请输入绑定客户&#39;,
        }
      }
    )
  },
  //表单提交
   formSubmit: function (e) {
     //提交错误描述
    if (!this.WxValidate.checkForm(e)) {
      const error = this.WxValidate.errorList[0]
      // `${error.param} : ${error.msg} `
      wx.showToast({
        title: `${error.msg} `,
        image: &#39;/pages/images/error.png&#39;,
        duration: 2000
      })
      return false
    }
    this.setData({ submitHidden: false })
    var that = this
    //提交
    wx.request({
      url: &#39;&#39;,
      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: &#39;POST&#39;,
      success: function (requestRes) {
        that.setData({ submitHidden: true })
        appInstance.userState.status = 0
        wx.navigateBack({
          delta: 1
        })
      },
      fail: function () {
      },
      complete: function () {
      }
    })
  }
Copy after login

Related recommendations:

Use JQuery to implement smart form validation function_jquery

Detailed example of CSS3 and HTML5 to implement form validation function

JavaScript Introduction to form verification function

How to implement WeChat applet payment and refund in php

Introduction example of WeChat applet development

The above is the detailed content of Complete example of WeChat mini program form verification function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!