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

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

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. pipreqs------查找python项目依赖并生成requirements.txt

    项目开发的时候,总是要搭建和部署环境,这时,就需要一个python第三方包的list,一般叫做requirements.txt.如果项目使用virtualenv环境,直接使用pip freeze即可, ...

  2. rest_frameword框架的基本组件

    序列化 序列化:转化数据和校验数据(提交数据时校验数据类型) 开发我们的Web API的第一件事是为我们的Web API提供一种将代码片段实例序列化和反序列化为诸如json之类的表示形式的方式.我们可 ...

  3. hdu 2215 & hdu 3932(最小覆盖圆)

    Maple trees Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. location.href、location.assign和location.replace的区别

    在写跳转页面的时候遇到个有意思的问题,RT的三个均能用来写跳转,总结了下它们之间的区别. 1.window.location.href=“url”;      改变url地址. location.hr ...

  5. Spark部署及应用

    在飞速发展的云计算大数据时代,Spark是继Hadoop之后,成为替代Hadoop的下一代云计算大数据核心技术,目前Spark已经构建了自己的整个大数据处理生态系统,如流处理.图技术.机器学习.NoS ...

  6. 【转】Python unittest数据驱动工具:DDT

    背景 python 的unittest 没有自带数据驱动功能. 所以如果使用unittest,同时又想使用数据驱动,那么就可以使用DDT来完成. DDT是 “Data-Driven Tests”的缩写 ...

  7. easyUI之datagrid

    对于easyUI中的datagrid组件,继承自pannel...,因此有其自己的组件,还有继承过来的属性. 1.width:指定其宽度 2.title:指定标题 3.iconCls:指定图标.见上图 ...

  8. CF 990A. Commentary Boxes【数学/模拟】

    [链接]:CF [题意]:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价. [分析]:分情况讨论,自己多举几个栗子. [代码]: #include<cstdio&g ...

  9. 51nod 子序列的个数(动态规划)

    子序列的个数 给定一个正整数序列,序列中元素的个数和元素值大小都不超过105, 求其所有子序列的个数.注意相同的只算一次:例如 {1,2,1}有子序列{1} {2} {1,2} {2,1}和{1,2, ...

  10. 1353表达式括号匹配(stack)

    [题目描述] 假设一个表达式有英文字母(小写).运算符(+,—,*,/)和左右小(圆)括号构成,以“@”作为表达式的结束符.请编写一个程序检查表达式中的左右圆括号是否匹配,若匹配,则返回“YES”:否 ...