微信小程序 - 表单验证插件WxValidate(自定义警告信息形式)
弹出的形式对于用户来说,总是不太友好的

可能会出现层级问题(只需要设置一下提示的层级即可)



WxValidate内置规则

以下代码拷贝即可使用~
wxml
<form bindsubmit='submitCheckInfo'> <!-- 提示警告! -->
<view class='wran-tips' style='{{displayWarn}}'>
<text>{{warnInfo}}</text>
</view> <view class="container">
<view class='container-info'>
<view class="man-form-info">
<view class='name'>姓名
<input placeholder='请输入姓名' name="name"></input>
</view>
<view class='idcard'>
身份证号码
<input maxlength='18' placeholder='请输入身份证号码' type='idcard' name="idcard"></input>
</view> <view class='phone'>
手机号码
<input maxlength='11' placeholder='请输入手机号码' type='number' bindinput="phoneInput" name="tel"></input>
</view> </view>
</view> <view class='read-man-pact'>
<checkbox-group name="assistance">
<checkbox></checkbox>
<navigator class='pact'>阅读《顺风男服务协议》</navigator>
</checkbox-group>
</view> <view class='submit-form-info'>
<button form-type='submit'>提交</button>
</view> </view>
</form>
js
import WxValidate from '../../../utils/WxValidate';
Page({
/**
* 页面的初始数据
*/
data: {
// 初始化警告
displayWarn: 'display:none'
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
// 校验规则 -rules
this.initValidate();
},
/**
* 表单验证->(可自定义验证形式)
*/
showWarnInfo(error) {
// 当前page是this对象
let page = this;
// 延时时间等待
let delayTime = 1;
// 延时等待毫秒,现设置为1000
let delayMillsecond = 1000;
// 调用显示警告函数
showWran(page, error, delayTime, delayMillsecond);
},
/**
* 表单-提交前的(校验)
*/
submitCheckInfo(e) {
const params = e.detail.value
console.log(params)
// 传入表单数据,调用验证方法
if (!this.WxValidate.checkForm(params)) {
const error = this.WxValidate.errorList[0]
this.showWarnInfo(error)
return false
}
// 验证通过以后
this.submitForm(params);
},
/**
* 表单-提交(到后端)
*/
submitForm(params) {
console.log(params);
wx.showToast({
title: '提交吧~Q!',
})
},
/**
* 表单-验证字段
*/
initValidate() {
const rules = {
name: {
required: true,
rangelength: [2, 4]
},
idcard: {
required: true,
idcard: true,
},
tel: {
required: true,
tel: true,
},
regcode: {
required: false,
minlength: 6
},
assistance: {
required: true,
assistance: true,
},
}
// 验证字段的提示信息,若不传则调用默认的信息
const messages = {
name: {
required: '请输入姓名',
rangelength: '请输入2~4个汉字个汉字'
},
tel: {
required: '请输入11位手机号码',
tel: '请输入正确的手机号码',
},
idcard: {
required: '请输入身份证号码',
idcard: '请输入正确的身份证号码',
},
regcode: {
required: '请输入验证码',
minlength: '请输入正确的验证码'
},
assistance: {
required: '请勾选 《顺风男服务协议》'
},
}
// 创建实例对象
this.WxValidate = new WxValidate(rules, messages)
// 自定义验证规则
this.WxValidate.addMethod('assistance', (value, param) => {
return this.WxValidate.optional(value) || (value.length >= 1 && value.length <= 2)
}, '请勾选 《顺风男服务协议》')
}
})
/**
* 可加入工具集-减少代码量
*/
function showWran(page, error, delayTime, delayMillsecond) {
let timesRun = 0;
let interval = setInterval(function() {
timesRun += delayTime;
if (timesRun === delayTime) {
clearInterval(interval);
}
page.setData({
warnInfo: error.msg,
displayWarn: 'display:none'
});
}, delayMillsecond);
page.setData({
warnInfo: error.msg,
displayWarn: 'display:block'
});
}
wxss
@import "../../template/up-pic.wxss";
page {
font-size: 30rpx;
}
button:active {
opacity: 0.7;
}
.wran-tips {
text-align: center;
color: #fff;
padding: 2%;
width: 100%;
background-color: #f00;
display: flex;
justify-content: center;
position: fixed;
top:;
}
.container-info {
padding: 5%;
/* margin-top: 4%; */
}
.man-form-info {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.man-form-info .name, .man-form-info .idcard, .man-form-info .phone,
.man-form-info .regcode {
display: flex;
width: 100%;
flex-wrap: wrap;
margin-top: 2%;
}
.man-form-info input {
width: 100%;
border-bottom: 1px solid #ddd;
}
.regcode {
position: relative;
}
.regcode button {
border-radius: 10rpx;
background-color: #3879d9;
color: #fff;
height: 54rpx;
line-height: 54rpx;
font-size: 23rpx;
width: 300rpx;
margin-top: -2%;
}
.regcode input {
/* width: 50%; */
width: 100%;
}
.code {
position: relative;
width: 100%;
}
.code button {
position: absolute;
top: 72rpx;
right:;
}
input:hover {
border-bottom: 2px solid #ddd;
}
.self-idcard-info {
margin-top: 15%;
display: flex;
flex-wrap: wrap;
justify-content: center;
width: 100%;
border: 1px dashed #ddd;
padding: 2%;
}
.f-center {
display: flex;
justify-content: center;
width: 100%;
}
.picture_list {
/* justify-content: center; */
padding: 0 7%;
}
.add-image {
background-color: #ddd;
color: #fff;
}
.upload_progress {
width: 167rpx;
height: 164rpx;
}
.apply {
width: 96%;
display: flex;
justify-content: space-between;
align-items: center;
padding: 2%;
border-top: 2px solid #ddd;
border-bottom: 2px solid #ddd;
}
.apply-deposit {
font-weight: bold;
}
.apply-deposit-amount {
font-weight: bold;
color: #fdd20c;
}
.apply button {
margin:;
padding:;
width: 240rpx;
height: 60rpx;
line-height: 60rpx;
color: #fff;
background-color: #fdd20c;
}
.read-man-pact {
display: flex;
justify-content: center;
padding: 2%;
}
.read-man-pact checkbox-group {
display: flex;
align-items: center;
}
.pact {
border-bottom: 1px solid #ddd;
}
.submit-form-info {
display: flex;
justify-content: center;
}
.submit-form-info button {
background-color: #fdd000;
width: 80%;
margin: 3% 0;
}
微信小程序 - 表单验证插件WxValidate(自定义警告信息形式)的更多相关文章
- 微信小程序——表单验证插件WxValidate的二次封装(二)
在上一篇博客<微信小程序——仿jqueryValidate表单验证插件WxValidate的二次封装>中,我将WxValidate做了再次封装,简化了初始规则数据的构造,但是当有错误时页面 ...
- 微信小程序——表单验证插件WxValidate的二次封装(终极版)
微信小程序表单验证前面的两篇文章做的效果总感觉都有点不太友好,第一篇里的效果是将错误信息通过对话框形式弹出来,这种形式在web形式下早已经淘汰了:第二篇是一次性全部显示所有的错误,然后3秒后自动消失, ...
- 微信小程序 - 表单验证插件WxValidate使用
插件下载地址及官方文档:https://github.com/skyvow/wx-extend 具体的WxValidate.js文件的位置在wx-extend/src/assets/plugins/w ...
- 微信小程序——仿jqueryValidate表单验证插件WxValidate的二次封装(一)
在做web开发时,表单验证插件我们前端用的是jqueryValidate,由于个人主要精力是在后台JAVA开发上,为了让插件与后台更好的结合和使用,通过JAVA的自定义组件将表单全部重新写了一边,同时 ...
- 微信小程序-表单
wxml <view> 按钮: <button size="{{buttom.size}}" type="{{buttom.type}}" p ...
- 微信小程序-表单组件
button 按钮 注:button-hover 默认为{background-color: rgba(0, 0, 0, 0.1); opacity: 0.7;} 示例代码: /** wxss **/ ...
- 微信小程序-表单笔记
发布页——向云端数据库上传多行文字和4张图片 第6,8行注释掉和不注释掉都可以实现数据上传 var _this = this; wx.cloud.callFunction({ name: 'searc ...
- 微信小程序 --- 表单输入验证(手机号、邮箱验证、输入非空)
js代码 Page({ /** * 页面的初始数据 */ data: { indicatorDots: false, ...
- 微信小程序-表单笔记2
本地添加4张图片并显示至页面——组件位置.设置样式.列表渲染 Q.button是一张图片,需要实现点击这张图片后选择本地图片后显示至页面,不知道怎么让本地图片将button挤到右边 S.在wxml中 ...
随机推荐
- P1709 [USACO5.5]隐藏口令Hidden Password
P1709 [USACO5.5]隐藏口令Hidden Password 题目描述 有时候程序员有很奇怪的方法来隐藏他们的口令.Binny会选择一个字符串S(由N个小写字母组成,5<=N<= ...
- Net Core 控制台程序使用Nlog 输出到log文件
using CoreImportDataApp.Common; using Microsoft.Extensions.Configuration; using Microsoft.Extensions ...
- python Tk()生成的桌面的具体设置方法
rom tkinter import * root = Tk() root['height'] = 300 #设置高 root['width'] = 500 #设置宽 root.title('魔方小站 ...
- Jquery实现全选和取消全选的方法
<input type="checkbox" id="all" />全选<br /> <input type="chec ...
- android studio 自定义路径安装报错"You are attempting to install the android SDK
android studio 自定义路径安装报错"You are attempting to install the android SDK 解决方法: 出现这个提示 主要是安装 Andro ...
- windows下tortoiseGit安装和使用
一.安装git for windows 首先下载git for windows客户端http://msysgit.github.io/ 安装过程没什么特别的,不停next就ok了 图太多就不继 ...
- MySQL 一般模糊查询的几种用法
1.%:表示零个或多个字符.在某些情况下需要中文查询,一般用两个%来查询,即%%: select * from user where name like %五%; -->表示:查询user表中的 ...
- 求第N个回文数 模板
备忘. /*看到n可以取到2*10^9.说明普通方法一个个暴力计算肯定会超时的,那打表呢?打表我们要先写个打表的代码,这里不提供.打完表观察数据,我们会发现数据其实是有规律的.完全不需要暴力的把所有数 ...
- The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - L Doki Doki Literature Club
Doki Doki Literature Club Time Limit: 1 Second Memory Limit: 65536 KB Doki Doki Literature Club ...
- 23、Flask实战第23天:Flask-Restful
Restful API规范 restful api是用于前端和后台进行通信的一套规范.使用这个规范可以让前后端开发变得更加轻松. 协议 采用http或者https 数据传输格式 数据之间传输的格式应该 ...