如下:
onLoad: function (options) {
this.initValidate()
this.initValidate2()
},
// 表单提交时的验证 : 账号登录方式
initValidate() {
let rules = {
username: {
required: true,
},
password: {
required: true,
},
captchaimg: {
required: true,
minlength: 4
},
}
let messages = {
username: {
required: '请输入用户名',
},
password: {
required: '请输入密码',
},
captchaimg: {
required: '请输入验证码',
minlength: '请输入4位的验证码',
},
}
console.info(rules)
this.wxValidate = new wx.wxValidate(rules, messages)
},
// 表单提交时的验证 : 手机号登录方式
initValidate2() {
let rules = {
mobile: {
required: true,
mobile: true
},
captcha: {
required: true,
minlength: 4
},
}
let messages = {
mobile: {
required: '请输入手机号码',
mobile: '手机号码格式不正确'
},
captcha: {
required: '请输入验证码',
minlength: '请输入4位的验证码',
},
}
this.wxValidate2 = new wx.wxValidate(rules, messages)
},
提交表单方法中用法:
if (!this.wxValidate1.checkForm(e.detail.value)) {
const error = this.wxValidate2.errorList[0]
wx.vantNotify({
type: 'warning',
message: error.msg
})
return false
}
if (!this.wxValidate2.checkForm(e.detail.value)) {
const error = this.wxValidate2.errorList[0]
wx.vantNotify({
type: 'warning',
message: error.msg
})
return false
}