首先准备第三方的WxValidate.js
https://github.com/skyvow/wx-extend/blob/master/docs/components/validate.md 下载地址
注意加载的时候要用到
用到index.js(自己定义的页面js)
import WxValidate from '../../../utils/WxValidate.js'
var Validate = ""
在onload 编辑代码
-
onLoad: function (options) {
-
// 页面初始化 options为页面跳转所带来的参数
-
var that = this
-
//数据进行校验
-
const rules = {
-
mobile: {
-
required: true,
-
tel: true,
-
},
-
consignee: {
-
required: true,
-
},
-
address: {
-
required: true,
-
minlength: 2,
-
},
-
zipcode: {
-
digits: true,
-
}
-
}
-
// 验证字段的提示信息,若不传则调用默认的信息
-
const messages = {
-
mobile: {
-
required: '请输入手机号码',
-
tel: '请输入正确的手机号码',
-
},
-
consignee: {
-
required: '请输入收货人',
-
},
-
address: {
-
required: '请输入收货地址',
-
minlength: '请输入正确的收货地址',
-
},
-
zipcode: {
-
digits: '请输入正确的邮编'
-
}
-
}
-
Validate = new WxValidate(rules,messages)
-
}
-
const params = event.detail.value
-
// 传入表单数据,调用验证方法
-
if (!Validate.checkForm(event)) {
-
const error = Validate.errorList[ 0]
-
//提示信息
-
-
return false
-
}
-