弹出的形式对于用户来说,总是不太友好的

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

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(自定义警告信息形式)的更多相关文章

  1. 微信小程序——表单验证插件WxValidate的二次封装(二)

    在上一篇博客<微信小程序——仿jqueryValidate表单验证插件WxValidate的二次封装>中,我将WxValidate做了再次封装,简化了初始规则数据的构造,但是当有错误时页面 ...

  2. 微信小程序——表单验证插件WxValidate的二次封装(终极版)

    微信小程序表单验证前面的两篇文章做的效果总感觉都有点不太友好,第一篇里的效果是将错误信息通过对话框形式弹出来,这种形式在web形式下早已经淘汰了:第二篇是一次性全部显示所有的错误,然后3秒后自动消失, ...

  3. 微信小程序 - 表单验证插件WxValidate使用

    插件下载地址及官方文档:https://github.com/skyvow/wx-extend 具体的WxValidate.js文件的位置在wx-extend/src/assets/plugins/w ...

  4. 微信小程序——仿jqueryValidate表单验证插件WxValidate的二次封装(一)

    在做web开发时,表单验证插件我们前端用的是jqueryValidate,由于个人主要精力是在后台JAVA开发上,为了让插件与后台更好的结合和使用,通过JAVA的自定义组件将表单全部重新写了一边,同时 ...

  5. 微信小程序-表单

    wxml <view> 按钮: <button size="{{buttom.size}}" type="{{buttom.type}}" p ...

  6. 微信小程序-表单组件

    button 按钮 注:button-hover 默认为{background-color: rgba(0, 0, 0, 0.1); opacity: 0.7;} 示例代码: /** wxss **/ ...

  7. 微信小程序-表单笔记

    发布页——向云端数据库上传多行文字和4张图片 第6,8行注释掉和不注释掉都可以实现数据上传 var _this = this; wx.cloud.callFunction({ name: 'searc ...

  8. 微信小程序 --- 表单输入验证(手机号、邮箱验证、输入非空)

    js代码 Page({                   /**    * 页面的初始数据    */         data: {         indicatorDots: false,   ...

  9. 微信小程序-表单笔记2

    本地添加4张图片并显示至页面——组件位置.设置样式.列表渲染 Q.button是一张图片,需要实现点击这张图片后选择本地图片后显示至页面,不知道怎么让本地图片将button挤到右边  S.在wxml中 ...

随机推荐

  1. AC日记——[JSOI2008]火星人prefix bzoj 1014

    1014 思路: 平衡树+二分答案+hash: 好了懂了吧. 代码: #include <cstdio> #include <cstring> #include <ios ...

  2. AC日记——病毒侵袭 hdu 2896

    2896 思路: 好题: 代码: #include <queue> #include <cstdio> #include <cstring> using names ...

  3. 跳转的两种实现方法setInterval和setTimeout

    setInterval方法: <html> <head> <meta http-equiv="Content-Type" content=" ...

  4. 最快的序列化组件protobuf的.net版本protobuf.net

    Protobuf是google开源的一个项目,用户数据序列化反序列化,google声称google的数据通信都是用该序列化方法.它比xml格式要少的多,甚至比二进制数据格式也小的多.     Prot ...

  5. 【cocos2d-js官方文档】十二、对象缓冲池

    cc.pool的使用场景 经常创建和销毁的元素,例如打飞机游戏里面的子弹等. 不适用的场景:不是很经常创建的物体,比如背景,建筑等. 如何使用cc.pool 让你的类支持cc.pool 首先,你需在需 ...

  6. docker 与 yarn

    有时我们的项目是使用yarn去发布的,当需要使用docker发布这个项目时,安装yarn是必须的,但是平时使用的npm install -g yarn此时却不可用 从网站上找到解决的方法 地址:htt ...

  7. HBASE启动失败,Failed construction of Master: class org.apache.hadoop.hbase.master.HMaster

    Master日志错误:2015-12-02 06:34:32,394 ERROR [main] master.HMasterCommandLine: Master exitingjava.lang.R ...

  8. tarjan算法与无向图的连通性(割点,桥,双连通分量,缩点)

    基本概念 给定无向连通图G = (V, E)割点:对于x∈V,从图中删去节点x以及所有与x关联的边之后,G分裂为两个或两个以上不相连的子图,则称x为割点割边(桥)若对于e∈E,从图中删去边e之后,G分 ...

  9. 树链剖分【p4114】Qtree-Query on a tree I

    Description 给定一棵n个节点的树,有两个操作: CHANGE i ti 把第i条边的边权变成ti QUERY a b 输出从a到b的路径中最大的边权,当a=b的时候,输出0 Input 第 ...

  10. leetcode122 Best Time to Buy and Sell Stock

    题意:有一个数组,第i个数据代表的是第i天股票的价格,每天只能先卖出再买进(可以不卖出也可以不买进),求最大收益. 思路:自己去弄几个数组比划比划就知道了,比如[1,2,5,3,6],第一天买进,第二 ...