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) => ...
随机推荐
- mapgis IGServer账号
2064803644@qq.com 1576391020@qq.com 密码一样
- django使用MySQL数据库
在实际生产环境,Django是不可能使用SQLite这种轻量级的基于文件的数据库作为生产数据库.一般较多的会选择MySQL. 下面介绍一下如何在Django中使用MySQL数据库. 一.安装MySQL ...
- axios写法
- 【java】Comparator的用法
文章转载自: http://blog.csdn.net/u012250875/article/details/55126531 1.为什么写? comparator 是javase中的接口,位于jav ...
- linux下编译C/C++ 程序
C/C++的速度是Python和perl所无法比拟的,尤其对于处理超大的生物信息学文件来说. 最近在写一个最简单的fastq cut工具,Python简直慢到不能忍,8G的fastq.gz文件的cut ...
- WPF经典编程模式-MVVM示例讲解
https://www.cnblogs.com/lvdongjie/p/5515962.html
- pandas更换index,column名称
1)仅换掉index名称 df.index = list 2)调整index时,后面的项目也要跟着调整: df.reindex(list) 注意如果list中出现了df中没有的index,后面的项目会 ...
- 关于ActionBar 左侧添加完返回后 点击无效的问题
ActionBar actionBar =getSupportActionBar(); if(actionBar!=null){ actionBar.setHomeAsUpIndicator(R.mi ...
- android -------- 解决NDK开发中的 Method 'NewStringUTF' could not be resolved
创建NDK项目时, .cpp文件中出现错误, Method 'NewStringUTF' could not be resolved 如图: 网上看了很多解决方式 项目右键->属性->c/ ...
- Linux中安装Mysql授权远程访问
一.直接授权 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youpassword' WITH GRANT OP ...