表单验证的应用场景十分广泛,因为网站对用户输入内容的限制是非常必要的。
在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()方法,表示在什么时候执行验证。

@blur="$v.user.Phone.$touch()"表示blur事件触发(失去焦点)时验证。
@input="$v.user.Phone.$touch()"表示input事件触发(输入内容发生变化)时验证。

转自;https://blog.csdn.net/latency_cheng/article/details/78580820

Vue 使用 vuelidate 实现表单验证的更多相关文章

  1. Vue如何使用vee-validate表单验证

    Vue项目遇到要表单验证了吧,对我来说表单验证是个很纠(dan)结(teng)的内容,各种判断凌乱到飞起.往常使用jquery的validate插件做表单验证方便吧,你也可以在Vue里引入jquery ...

  2. 关于vue.js element ui 表单验证 this.$refs[formName].validate()的问题

        方法使用前需了解: 来自”和“小编的小提示: 首先打印一下this.$refs[formName],检查是否拿到了正确的需要验证的form. 其次在拿到了正确的form后,检查该form上添加 ...

  3. vue问题六:表单验证

    表单验证规则 查看文档:https://blog.csdn.net/weixin_42018790/article/details/80762149 1). el-form增加 :rules=&quo ...

  4. vue 中跨组件的表单验证

    使用的是element写的,里面提供了表单验证. 子组件是这样的 <template> <div> <el-form :model="value" r ...

  5. vue elementUI之Form表单 验证

    首先说一下 我在form表单里面遇见的坑: 1.例如我要给后台传的不是对象,而是一个数组,怎么写验证? 2.比如我有四个弹出框,都要做验证,这个时候就要注意了,每一个弹出框的ref都不能给的一样,并且 ...

  6. vue 新增时清除表单验证注意事项

    // 清除表单校验的提示 if (this.$refs['XXX']) { // 延时执行 this.$nextTick(function () { this.$refs['XXX'].clearVa ...

  7. vue+element-ui中的表单验证(电话等等)

    1. 2. 3. ============================================================上代码============================ ...

  8. vue中使用iview表单验证时this指针问题

    需求 使用iview,在提交时对值b进行验证,使其不能大于值a 实现 <Form ref="config" :model="config" :rules= ...

  9. vue+element之多表单验证

    方法一:利用promise var p1=new Promise(function(resolve, reject) { this.$refs[form1].validate((valid) => ...

随机推荐

  1. 【Selenium2】【环境搭建】

    Windows7  64位 Mozilla Firefox 36.0.4 + Firebug 2.0.19 + FirePath 0.9.7.1.1-signed.1-signed 火狐历史版本:ht ...

  2. dataTables使用整理(一)

    初次使用dataTables,对一些用到的属性及遇到的问题做一个简要的记录 参考资料: http://blog.csdn.net/mickey_miki/article/details/8240477 ...

  3. P1182 数列分段`Section II`

    传送门 思路: 求数列每段和的最大值的最小值,很明显是用二分法求解,加贪心检验.本题关键是要怎么去高效的check,可以考虑一个贪心的思路,能加的就加上,不能则新开一段,so对于二分的值 u ,我们从 ...

  4. maven项目, 单元测试失败提示 Class not found datastorage........

    ---恢复内容开始--- 单元测试失败:  提示 Class not found datastorage........ 原因:   maven  环境变量问题,   eclipse 没有自动更新下载 ...

  5. Linux修改hostname时/etc/hosts、/etc/sysconfig/network ,hostname,三者的区别和联系

    [root@localhost /]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.l ...

  6. 对Maven项目进行强制更新

    选中项目 然后 选中Force Update of Snapshots/Releases,点击OK即可. 此时pom.xml就不会报错了.

  7. js中use或者using方法

    看Vue.use方法,想起了以前工作中别人用过的use方法. var YANMethod = { using:function() { var a = arguments, o = this, i = ...

  8. Ubuntu终端多窗口分屏Terminator

    1.安装 Terminator最大的特点就是可以在一个窗口中打开多个终端 sudo apt-get install terminator 2.快捷键 Ctrl+Shift+E    垂直分割窗口 Ct ...

  9. English trip V1 - B 1. How much is it? 它是多少钱? Teacher:Corrine Key: is/are

    In this lesson you will learn to ask about prices. 本节课你将学习询问关于价格 课上内容(Lesson) one piece of     two p ...

  10. English trip V1 - 19.Where Am I? 我在哪里?Teacher:Patrick Key:Ask for and directions

    In this  lesson you will learn to ask for and give directions. 本节课你将学习到学会问路和指路. 课上内容(Lesson) 人类的几种感: ...