Vue 使用 vuelidate 实现表单验证
表单验证的应用场景十分广泛,因为网站对用户输入内容的限制是非常必要的。
在vue中,我们使用vuelidate方便地实现表单验证。
官方文档在这里https://monterail.github.io/vuelidate/
1、安装
- 使用npm安装:npm install vuelidate --save
- 在main.js中引入:
import Vuelidate from 'vuelidate'
Vue.use(Vuelidate)
在组件.vue中引入需要使用的验证项:
import { required, minLength, maxLength, sameAs } from 'vuelidate/lib/validators'
2、使用
例如我们要写一个注册时的表单验证,需要用户填写的信息有:username,phoneNumber,phoneCode,password,confirmPassword。用vuelidate对这些数据进行验证。代码很容易懂,就不写多余的说明了。
.vue中的代码如下:
data () {
return {
user: {
username:'',
phone: '',
phoneCode: '',
password: '',
confirmPassword: '',
},
}
},
validations: {
user: {
username: {
required,
minLength: minLength(2),
maxLength: maxLength(20)
}
phone: {
required
},
phoneCode: {
required
},
password: {
required,
minLength: minLength(8),
maxLength: maxLength(32)
},
confirmPassword: {
sameAsPassword: sameAs('Password')
}
}
}
截取HTML中手机号,密码和验证密码的代码如下:
<!--手机号-->
<div class="form-group" :class="{'error': $v.user.phone.$error}">
<span class="message" v-if="!$v.user.phone.required">手机号不能为空</span> <input type="text" placeholder="手机号"
v-model.trim="user.phone"
@blur="$v.user.phone.$touch()">
</div> <!--密码-->
<div class="form-group" :class="{'error': $v.user.password.$error}">
<span class="message" v-if="!$v.user.password.required">密码不能为空</span>
<span class="message"
v-if="!$v.user.password.minLength">密码不能小于{{$v.user.password.$params.minLength.min}}位</span>
<span class="message"
v-if="!$v.user.password.maxLength">密码不能大于{{$v.user.password.$params.maxLength.max}}位</span> <div class="auth-password">
<input type="password" placeholder="输入密码"
v-model.trim="user.password"
@blur="$v.user.password.$touch()" ref="password1">
</div>
</div> <!--确认密码-->
<div class="form-group" :class="{'error': $v.user.confirmPassword.$error}">
<span class="message" v-if="!$v.user.confirmPassword.sameAsPassword">两次输入的密码不一样</span> <div class="auth-password">
<input type="password" placeholder="确认密码"
v-model.trim="user.confirmPassword"
@blur="$v.user.confirmPassword.$touch()"
@keyup.enter="register" ref="password2">
</div>
</div>
其中,第21行、32行中的$touch()方法,表示在什么时候执行验证。
转自;https://blog.csdn.net/latency_cheng/article/details/78580820
Vue 使用 vuelidate 实现表单验证的更多相关文章
- Vue如何使用vee-validate表单验证
Vue项目遇到要表单验证了吧,对我来说表单验证是个很纠(dan)结(teng)的内容,各种判断凌乱到飞起.往常使用jquery的validate插件做表单验证方便吧,你也可以在Vue里引入jquery ...
- 关于vue.js element ui 表单验证 this.$refs[formName].validate()的问题
方法使用前需了解: 来自”和“小编的小提示: 首先打印一下this.$refs[formName],检查是否拿到了正确的需要验证的form. 其次在拿到了正确的form后,检查该form上添加 ...
- vue问题六:表单验证
表单验证规则 查看文档:https://blog.csdn.net/weixin_42018790/article/details/80762149 1). el-form增加 :rules=&quo ...
- vue 中跨组件的表单验证
使用的是element写的,里面提供了表单验证. 子组件是这样的 <template> <div> <el-form :model="value" r ...
- vue elementUI之Form表单 验证
首先说一下 我在form表单里面遇见的坑: 1.例如我要给后台传的不是对象,而是一个数组,怎么写验证? 2.比如我有四个弹出框,都要做验证,这个时候就要注意了,每一个弹出框的ref都不能给的一样,并且 ...
- vue 新增时清除表单验证注意事项
// 清除表单校验的提示 if (this.$refs['XXX']) { // 延时执行 this.$nextTick(function () { this.$refs['XXX'].clearVa ...
- vue+element-ui中的表单验证(电话等等)
1. 2. 3. ============================================================上代码============================ ...
- vue中使用iview表单验证时this指针问题
需求 使用iview,在提交时对值b进行验证,使其不能大于值a 实现 <Form ref="config" :model="config" :rules= ...
- vue+element之多表单验证
方法一:利用promise var p1=new Promise(function(resolve, reject) { this.$refs[form1].validate((valid) => ...
随机推荐
- Java 数据库篇
一.简易封装JDBC工具类: package com.jackie.MyBatis.main; import java.sql.Connection; import java.sql.DriverMa ...
- 理解ffmpeg中的pts,dts,time_base
首先介绍下概念: PTS:Presentation Time Stamp.PTS主要用于度量解码后的视频帧什么时候被显示出来 DTS:Decode Time Stamp.DTS主要是标识读入内存中的b ...
- linux系统下各类软件安装笔记
安装环境: linux版本:ubuntu 16.04 安装python3.6 sudo add-apt-repository ppa:jonathonf/python-3.6 ...
- Centos7 安装python3.7.2
下载python3.7.2源码 wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz 下载完后对压缩包解压缩 tar -xf Py ...
- QT文件(夹)操作---QFile、QDir、QFileInfo、QTextStream和QDataStream异同
1.1 文件和目录 QFile.QBuffer和QTcpSocket可支持读写设备,用open函数打开,用write或putChar函数写入.用read和readLine或readAll进行读取 ...
- Java HashMap 遍历、删除、排序
首先创建一个map对象,并依次放入几个测试数据 HashMap<String, Integer> map = new HashMap<String, Integer>(); m ...
- js正则表达式的积累
验证数字:^[0-9]*$ 验证n位的数字:^\d{n}$ 验证至少n位数字:^\d{n,}$ 验证m-n位的数字:^\d{m,n}$ 验证零和非零开头的数字:^(0|[1-9][0-9]*)$ 验证 ...
- 反转链表 Reverse Linked List
2018-09-11 22:58:29 一.Reverse Linked List 问题描述: 问题求解: 解法一:Iteratively,不断执行插入操作. public ListNode reve ...
- (转)C# Stopwatch详解
C# Stopwatch类 命名空间:System.Diagnostics; namespace System.Diagnostics { // 提供一组方法和属性,可用于准确地测量运行时间. pub ...
- 【转】 strrchr()函数---C语言
转自:https://baike.baidu.com/item/strrchr/4621437?fr=aladdin 函数名称: strrchr 函数原型:char *strrchr(const ...