栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

微信小程序表单验证功能完整实例

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微信小程序表单验证功能完整实例

本文实例讲述了微信小程序表单验证功能。分享给大家供大家参考,具体如下:

Wxml




app.js

import wxValidate from 'utils/wxValidate'
App({
  wxValidate: (rules, messages) => new wxValidate(rules, messages)
})

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: '请填写您的姓名姓名',
 },
 mobile: {
   required: '请填写您的手机号',
 },
 company: {
   required: '请输入公司名称',
 },
 client: {
   required: '请输入绑定客户',
 }
      }
    )
  },
  //表单提交
   formSubmit: function (e) {
     //提交错误描述
    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
    //提交
    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 () {
      }
    })
  }

WxValidate.js 文件点击此处本站下载

希望本文所述对大家微信小程序开发有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/83610.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号