官方文档:http://vuejs.github.io/vue-validator/zh-cn/index.html

github项目地址:https://github.com/vuejs/vue-validator

单独使用vue-validator的方法见官方文档,本文结合vue-router使用。

安装验证器

不添加自定义验证器或者无需全局使用的公用验证器,在main.js中安装验证器,使用 CommonJS 模块规范, 需要显式的使用 Vue.use() 安装验证器组件。

import Validator from 'vue-validator'
Vue.use(Validator)

与 vue-router 同时使用,必须在调用 router#map, router#start 等实例方法前安装验证。

若要自定义验证器,建一个js文件,在该文件中安装验证器组件。例如:validation.js

import Vue from 'vue'
import Validator from 'vue-validator'
Vue.use(Validator)
//自定义验证器

自定义验证器

官方提供的api如下

input[type="text"]
input[type="radio"]
input[type="checkbox"]
input[type="number"]
input[type="password"]
input[type="email"]
input[type="tel"]
input[type="url"]
select
textarea

但是以上的不一定满足我们的需求,这时就需要用到另一个全局api,用于注册和获取全局验证器。

Vue.validator( id, [definition] )

示例  定义validation.js  内容如下

import Vue from 'vue'
import Validator from 'vue-validator'
Vue.use(Validator)
//自定义验证器
//添加一个简单的手机号验证
//匹配0-9之间的数字,并且长度是11位
Vue.validator('tel', function (val) {
return /^[0-9]{11}$/.test(val)
});
//添加一个密码验证
//匹配6-20位的任何字类字符,包括下划线。与“[A-Za-z0-9_]”等效。
Vue.validator('passw', function (val) {
return /^(\w){6,20}$/.test(val)
});

使用验证器

验证器语法

<validator name="validation">
<input type="text" v-model='comment' id='comment' v-validate:comment="{ minlength: 3, maxlength: 15 }">
<div>
<span v-show="$validation.comment.minlength">不得少于3个字符</span>
<span v-show="$validation.comment.maxlength">不得大于15个字符</span>
</div>
</validator>

默认情况下,vue-validator 会根据 validator 和 v-validate 指令自动进行验证。然而有时候我们需要关闭自动验证,在有需要时手动触发验证。如果你不需要自动验证,可以通过 initial 属性或 v-validate 验证规则来关闭自动验证。如下:

<validator name="validation">
<input type="text" v-model='comment' id='comment' v-validate:comment="{ minlength: 3, maxlength: 15 }" detect-change="off" initial='off'>
<div>
<span v-show="$validation.comment.minlength">不得少于3个字符</span>
<span v-show="$validation.comment.maxlength">不得大于15个字符</span>
</div>
</validator>

Terminal 指令问题

<validator name="test_validator">
<!-- @invalid:valid的逆 ,表示验证不通过 -->
<input @invalid="passwInvalid" @valid="passwok" type="password" v-model='passw' id='passw' v-validate:passw="['passw']" detect-change="off" initial='off' placeholder='请输入密码'>
 <input @invalid="passwInvalid" @valid="passwok" type="password" v-model='passw2' id='passw2' v-validate:passw2="['passw']" detect-change="off" initial='off' placeholder='请输入密码'>
</validator>
<script>
//若是在main.js中导入 无需再次导入
//此处导入的是上面代码的validation.js
import validator from '../validator/validation'
export default{
data(){
return{
comment:'',
passw:'',
passw2:''
}
},
methods:{
passwInvalid(){
alert('只能输入6-20个字母、数字、下划线');
},
passwok(){
//alert('验证码符合规范')
}
}
}
</script>

 示例:用户注册验证

用了一个组件来显示提示信息

toast.vue

<template>
<div v-show="toastshow" transition="toast" class="toast font-normal">
{{toasttext}}
</div>
</template>
<script>
export default{
props:{
//是否显示提示
toastshow:{
type:Boolean,
required: false,
default:function(){
return false;
}
},
//提示的内容
toasttext:{
type:String,
required: false,
default:function(){
return 'no message';
}
},
//显示的时间
duration: {
type: Number,
default:3000,//默认3秒
required:false
}
},
ready() { },
watch:{
toastshow(val){
if (this._timeout) clearTimeout(this._timeout)
if (val && !!this.duration) {
this._timeout = setTimeout(()=> this.toastshow = false, this.duration)
}
}
}
}
</script>
<style>
.toast{
position:absolute;
left:50%;
margin-left:-25%;
bottom:30px;
display:block;
width:200px;
height:auto;
text-align:center;
color:white;
background-color:rgba(0,0,0,0.5);
border-radius:10px;
z-index:10;
transform:scale(1);
padding:5px;
}
.toast-transition{
transition: all .3s ease;
}
.toast-enter{
opacity:0;
transform:scale(0.1);
}
.toast-leave{
opacity:0;
transform:scale(0.1);
}
</style>

注册用户:假如我们需要填写手机号和输入两次密码

<template>
<div class='register-box'>
<!-- 组件:用于显示提示信息 -->
<Toast :toastshow.sync="toastshow" :toasttext="toasttext"></Toast>
<validator name="validation_register1">
<div class='register1'>
<div class='pd05'>
<input @invalid="telonInvalid" initial="off" detect-change="off" v-model="telphone" id="telphone" type="tel" class='phone-number' v-validate:telphone="['tel']" placeholder='请输入手机号码'>
</div>
<div class='pd05'>
<input @invalid="passwInvalid" v-model="passw1" initial="off" detect-change="off" id="passw1" type="password" v-validate:passw1="['passw']" class='password-number' placeholder='请输入密码'>
</div>
<div class='pd05'>
<input @invalid="passwInvalid" v-model="passw2" initial="off" detect-change="off" id="passw2" type="password" v-validate:passw2="['passw']" class='password-number' placeholder='请输入密码'>
</div>
<a class='greenBtn' v-on:click='register_user()'>下一步</a>
</div>
</validator>
</div>
</template>
<script>
//导入validation.js  此处的validation.js就是上文中validation.js的内容
import validator from '../validator/validation';
//导入显示提示信息的组件
import Toast from '../components/toast.vue';
export default{
components: {
//注册组件
Toast
},
data(){
return{
telphone:'',//电话号码
toastshow:false,//默认不现实提示信息
toasttext:'',//提示信息内容
passw1:'',//首次输入密码
passw2:''//再次输入密码
}
},
methods:{
//手机号验证失败时执行的方法
telonInvalid(){
//设置提示信息内容
this.$set('toasttext','手机不正确');
//显示提示信息组件
this.$set('toastshow',true);
},
//密码验证失败时执行的方法
passwInvalid(){
this.$set('toasttext','只能输入6-20个字母、数字、下划线');
this.$set('toastshow',true);
},
register_user(){
var that = this;
var telephones = that.$get('telphone');
var pw1 = that.$get('passw1');
var pw2 = that.$get('passw2')
that.$validate(true, function () {
if (that.$validation_register1.invalid) {
//验证无效
that.$set('toasttext','请完善表单');
that.$set('toastshow',true);
}else{
that.$set('toasttext','验证通过');
that.$set('toastshow',true);
//验证通过做注册请求
/*that.$http.post('http://192.168.30.235:9999/rest/user/register',{'account':telephones,'pwd':pw1,'pwd2':pw2}).then(function(data){
if(data.data.code == '0'){
that.$set('toasttext','注册成功');
that.$set('toastshow',true);
}else{
that.$set('toasttext','注册失败');
that.$set('toastshow',true);
}
},function(error){
//显示返回的错误信息
that.$set('toasttext',String(error.status));
that.$set('toastshow',true);
})*/
}
}) }
}
}
</script>
<style>
.register-box{
padding: 10px;
}
.pd05{
margin-top: 5px;
}
.greenBtn{
width: 173px;
height: 30px;
text-align: center;
line-height: 30px;
background: red;
color: #fff;
margin-top: 5px;
}
</style>

若点击下一步,会提示“请完善表单”,因为验证不通过;若是文本框获得焦点后失去焦点则会提示相应的错误信息;若内容填写正确,则会提示验证通过并发送相应的请求。

效果如图

vue-validator(vue验证器)的更多相关文章

  1. [LeetCode] Tag Validator 标签验证器

    Given a string representing a code snippet, you need to implement a tag validator to parse the code ...

  2. validator js验证器

    转自:https://github.com/jaywcjlove/validator.js 轻量级的JavaScript表单验证,字符串验证.没有依赖,支持UMD 导入js库 <script t ...

  3. vue props 下有验证器 validator 验证数据返回true false后,false给default值

    vue props 下有验证器 validator 验证数据返回true false后,false给default值 props: { type: { validator (value) { retu ...

  4. VUE验证器哪家强? VeeValidate absolutely!

    VUE验证器哪家强? VeeValidate absolutely! vee-validate表单验证用法 github地址:https://github.com/baianat/vee-valida ...

  5. vue表单验证--veevalidate使用教程

    vue表单验证----vee-validate使用教程 官网:https://baianat.github.io/vee-validate/ 一.安装 npm install vee-validate ...

  6. vue 表单验证实例

    1.注册 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  7. vue表单验证:vee-validate中文提示

    官方文档:https://baianat.github.io/vee-validate/guide/ vee-validate可用于vue项目中进行表单验证,使用方法在官方API上都可以查到: 使用过 ...

  8. 【Vue】Vue框架常用知识点 Vue的模板语法、计算属性与侦听器、条件渲染、列表渲染、Class与Style绑定介绍与基本的用法

    Vue框架常用知识点 文章目录 Vue框架常用知识点 知识点解释 第一个vue应用 模板语法 计算属性与侦听器 条件渲染.列表渲染.Class与Style绑定 知识点解释 vue框架知识体系 [1]基 ...

  9. Vue 表单验证插件

    verify github:https://github.com/liuyinglong/verifynpm:https://www.npmjs.com/package/vue-verify-plug ...

随机推荐

  1. linux(十三)__vsftpd服务器

    rpm -qa |grep vsftpd yum search vsftpd yum install vsftpd 查看是否已经启动: service vsftpd status 启动: servic ...

  2. MyEclipse相关部署问题

    部署Tomcat如果用MyEclipse自动部署方式很有可能出现一个问题: 服务器关联的这个项目却关联到其他的项目上,导致运行时还在运行以前的项目 解决方法: 将状态提示栏的service里的tomc ...

  3. C++01.类的引入

    1.假设我们要输出张三,李四两个人的基本信息,包括姓名,年龄,可以用以下的C程序实现: eg: #include <stdio.h> int main(int argc,char **ar ...

  4. video/audio在ios/android上播放兼容

    1.audio自动播放 <audio src='xxx.mp3' autoplay></audio> 上面是audio标签autoplay属性是自动播放,但是在安卓部分浏览器和 ...

  5. 北京54全国80及WGS84坐标系的相互转换

    这三个坐标系统是当前国内较为常用的,它们均采用不同的椭球基准.其中北京54坐标系,属三心坐标系,大地原点在苏联的普而科沃,长轴6378245m,短轴6356863,扁率1/298.3:西安80坐标系, ...

  6. Android开发案例 - 注册登录

    本文只涉及UI方面的内容, 如果您是希望了解非UI方面的访客, 请跳过此文. 在微博, 微信等App的注册登录过程中有这样的交互场景(如下图): 打开登录界面 在登录界面中, 点击注册, 跳转到注册界 ...

  7. Action.c(58): Error -27796: Failed to connect to server "hostname"

    分析: 因为负载生成器的性能太好发数据特别快,服务器响应也特别快,从而导致负载生成器的端口在没有timeout之前就全部占满了. 解决方案一:   在负载生成器的注册表HKEY_LOCAL_MACHI ...

  8. ASP.NET MVC 3 网站优化总结(一) 使用 Gzip 压缩

    网站开启 Gzip 压缩的好处相信很多人都已经清楚,这样做可以提高网站的性能.那么为什么很多网站没有开启 Gzip 压缩功能呢?原因有4点:防病毒软件.浏览器 bug.网站代理和服务器未配置. 使用 ...

  9. 原创 C++应用程序在Windows下的编译、链接:第二部分COFF/PE文件结构

    2.1概述 在windows操作系统下,可执行文件的存储格式是PE格式:在Linux操作系统下,可执行文件的存储格式的WLF格式.它们都是COFF格式文件的变种,都是从COFF格式的文件演化而来的. ...

  10. win 7下建立FTP

    1.安装FTP服务 鼠标桌面右击个性化-卸载程序-打开或关闭windows功能 2.在IIS控制面板里添加FTP站点 下一步 下一步 鼠标右击 下一步 下一步 3.配置ftp站点 4.测试站点是否正常 ...