微信小程序WxValidate插件的密码验证

wxml

<view class="section__title">密码</view>
<input name="password" id="password" placeholder="请输入密码" value='{{form.password}}'/>
<view class="section__title">确认密码</view>
<input name="checkPassword" id="checkPassword" placeholder="请再次输入密码" value="{{form.checkPassword}}"/>
 
js
import WxValidate from "../../utils/WxValidate.js";
// pages/verify/verify.js
Page({

/**
* 页面的初始数据
*/
data: {
form:{
password:"",
checkPassword:"",
}
},
 
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options){
this.initValidate()//验证规则函数
},
//提交表单
formSubmit(e){
console.log(e.detail.value)
},
//报错
showModal(error) {
wx.showModal({
content:error.msg,
showModal:false,
})
},
//验证函数
initValidate(){
//规则
const rules = {
password:{
required: true,
minlength:6,
maxlength:16,
},
checkPassword:{
required:true,
equalTo: 'password',
}
}
//返回信息
const messages = {
password:{
required:'请填写密码',
minlength:'密码长度不能少于6位',
maxlength:'密码长度不能超过16位'
},
checkPassword:{
required:'请填写确认密码',
equalTo:'两次输入的密码不一致'
}
}
this.WxValidate = new WxValidate(rules,messages)
},
//调用验证函数
formSubmit(e){
// this.data.form.password = 13456
// this.setData({
// form:this.data.form
// })
console.log(e.detail.value)
console.log(this.data)
const params = e.detail.value
//校验表单
if(!this.WxValidate.checkForm(params)){
const error = this.WxValidate.errorList[0]
this.showModal(error)
return false
}
this.showModal({
msg:'提交成功'
})
}
})
 

转载于:https://www.cnblogs.com/wugai/p/10918487.html

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Vant 的 wxValidate 插件可以方便地实现表单验证。下面是具体步骤: 1. 安装 wxValidate 插件 在小程序项目中,执行以下命令安装 wxValidate 插件: ``` npm install --save @vant/weapp/wxValidate ``` 2. 引入 wxValidate 插件 在需要使用表单验证的页面中,引入 wxValidate 插件: ``` import WxValidate from '@vant/weapp/wxValidate'; ``` 3. 初始化 wxValidate 在页面的 onLoad 方法中,初始化 wxValidate: ``` onLoad() { this.initValidate(); }, initValidate() { const rules = { name: { required: true, minlength: 2, maxlength: 10 }, age: { required: true, number: true, range: [18, 60] }, email: { required: true, email: true } }; const messages = { name: { required: '请输入姓名', minlength: '姓名长度不能少于2个字符', maxlength: '姓名长度不能大于10个字符' }, age: { required: '请输入年龄', number: '年龄必须为数字', range: '年龄必须在18到60之间' }, email: { required: '请输入邮箱', email: '邮箱格式不正确' } }; this.WxValidate = new WxValidate(rules, messages); } ``` 在上面的示例中,定义了三个表单字段的验证规则和提示信息,分别是姓名、年龄和邮箱。 4. 表单提交时进行验证 在表单提交时,调用 wxValidate 的 checkForm 方法进行验证: ``` onSubmit() { if (!this.WxValidate.checkForm(this.data)) { const error = this.WxValidate.errorList[0]; wx.showToast({ title: error.msg, icon: 'none' }); return false; } wx.showToast({ title: '提交成功', icon: 'success' }); } ``` 在上面的示例中,调用 checkForm 方法进行表单验证,如果验证不通过,则弹出提示信息;如果验证通过,则提交表单数据。 以上就是使用 Vant 的 wxValidate 插件实现表单验证的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值