小程序模板網(wǎng)

微信小程序開發(fā)—表單驗(yàn)證

發(fā)布時(shí)間:2017-12-12 17:31 所屬欄目:小程序開發(fā)教程

Wxml

<form bindsubmit="formSubmit" bindreset="formReset">
  <input name="name" class="{{whoClass=='name'?'placeholderClass':'inputClass'}}" placeholder="請?zhí)顚懩男彰?quot; type="text" confirm-type="next" focus="{{whoFocus=='name'?true:false}}" bindblur="nameBlurFocus" />
  <radio-group name="gender" bindchange="radioChange">
    <radio value="0" checked />女士
    <radio value="1" />先生
  </radio-group>
  <input name="mobile" class="{{whoClass=='mobile'?'placeholderClass':'inputClass'}}" type="number" maxlength="11" placeholder="請?zhí)顚懩氖謾C(jī)號" bindblur="mobileBlurFocus" focus="{{whoFocus=='mobile'?true:false}}" />
  <input name="company" class="{{whoClass=='company'?'placeholderClass':'inputClass'}}" placeholder="公司名稱" type="text" confirm-type="next" focus="{{whoFocus=='company'?true:false}}" />
  <input name="client" class="{{whoClass=='client'?'placeholderClass':'inputClass'}}" placeholder="綁定客戶" type="text" confirm-type="done" focus="{{whoFocus=='client'?true:false}}" />
  <button formType="submit">提交</button>
</form>
<loading hidden="{{submitHidden}}">
  正在提交...
</loading>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

app.js

import wxValidate from 'utils/wxValidate'
App({
    wxValidate: (rules, messages) => new wxValidate(rules, messages)
})
  • 1
  • 2
  • 3
  • 4

news.js

var appInstance = getApp()
//表單驗(yàn)證初始化
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: '請?zhí)顚懩男彰彰?#39;,
                },
                mobile: {
                    required: '請?zhí)顚懩氖謾C(jī)號',
                },
                company: {
                    required: '請輸入公司名稱',
                },
                client: {
                    required: '請輸入綁定客戶',
                }
            }
        )
    },
    //表單提交
     formSubmit: function (e) {
         //提交錯(cuò)誤描述
        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 () {
            }
        })
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

WxValidate 鏈接: https://pan.baidu.com/s/1hsC9Xxq 密碼: ttjg


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉庫:starfork
本文地址:http://22321a.com/wxmini/doc/course/18139.html 復(fù)制鏈接 如需定制請聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢