这篇“Vue Validator表单验证插件怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Vue Validator表单验证插件怎么使用”文章吧。
基本使用
<div id="app"> <validator name="validation"> <form novalidate> <div class="username-field"> <label for="username">username:</label> <input type="text" id="username" v-validate:username="['required']" /> </div> <div class="comment-filed"> <label for="comment">comment:</label> <input type="text" id="comment" v-validate:comment="{maxlength: 256}" /> </div> <div class="errors"> <p v-if="$validation.username.required">请输入你的名字</p> <p v-if="$validation.comment.maxlength">您的评论太长了</p> </div> <input type="submit" value="send" v-if="$validation.valid" /> </form> </validator> </div> <script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script> <script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: '#app' }); </script>
将要验证的表单包裹在validator自定义元素指令中,而在要验证的表单控件元素的 v-validate 属性上绑定相应的校验规则。
验证结果会保存在组建实例的 $validation 属性下。 $validation 是由 validator 元素和 name 属性和 $ 前缀组件
验证结果结构
{ // 表单整体验证 "valid": false, // 字段校验是否通过 "invalid": true, // valid 取反 "touched": false, // 校验字段所在元素获得通过焦点时返回true,否则返回false "untouched": true, // touched 取反 "modified": false, // 当元素值与初始值不同时返回true,否则返回false "dirty": false, // 字段值改变过至少一次返回true,否则返回false "pristine": true, // dirty 取反 // 字段单一验证 "username": { "required": true, "modified": false, "pristine": true, "dirty": false, "untouched": true, "touched": false, "invalid": true, "valid": false }, "comment": { "maxlength": false, "modified": false, "pristine": true, "dirty": false, "untouched": true, "touched": false, "invalid": false, "valid": true } }
校验结果由两部分组成。表单整体校验结果和单个字段校验结果。
验证器语法
v-validate 指令语法:
v-validate[:field]=”array literal | object literfal | binding”
校验字段名field
field用来标识校验字段,之后可以用该字段来引用校验结果
v-validate 指令用来定义校验规则,其值可以是数组字面量,对象字面量,组件实例数组属性名。
数组字面量
当校验器不需要额外参数时,可以使用数组字面量形式,如 required 校验器,只要出现就带I表该校验器所在元素是必填项。
<div id="app"> <validator name="validation"> <form novalidate> Zip: <input type="text" v-validate:zip="['required']" /><br /> <div> <span v-if="$validation.zip.required">邮政编码是必填项</span> </div> </form> </validator> </div>
对象字面量
对象字面量语法适合需要额外参数的校验器。如限制输入长度的校验器 minlength,需要说明限制长度多少。
<div id="app"> <validator name="validation"> <form novalidate> ID: <input type="text" v-validate:id="{ required:true, minlength: 3, maxlength: 16 }" /> <br /> <div> <p v-if="$validation.id.required">ID不能为空</p> <p v-if="$validation.id.minlength">你的ID名字太短</p> <p v-if="$validation.id.maxlength">你的ID名字太长</p> </div> <input type="submit" value="send" v-if="$validation.valid" /> </form> </validator> </div>
还可以用 对象字面量语法通过 rule 字段来自定义验证规则
<div id="app"> <validator name="validation"> <form novalidate> ID: <input type="text" v-validate:id="{minlength: {rule: 3}, required: true, maxlength: {rule: 16}}" /> <br /> <div> <p v-if="$validation.id.required">ID不能为空</p> <p v-if="$validation.id.minlength">你的ID名字太短</p> <p v-if="$validation.id.maxlength">你的ID名字太长</p> </div> <input type="submit" value="send" v-if="$validation.valid" /> </form> </validator> </div>
实例数据属性
v-validate 的值可以是组建实例的数据属性。这样可以用来动态绑定校验规则。
<div id="app"> <validator name="validation"> <form novalidate> ID: <input type="text" v-validate:id="rules" /><br /> <div> <p v-if="$validation.id.required">不能为空</p> <p v-if="$validation.id.minlength">你的ID太短</p> <p v-if="$validation.id.maxlength">你的ID太长</p> </div> </form> </validator> </div> <script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script> <script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: '#app', data: { rules: { required: true, minlength: 3, maxlength: 16 } } }); </script>
内置校验规则
vue-validator 内置一些常用的验证规则:
required — 输入值不能为空
pattern — 必须匹配pattern表示的正则表达式
minlength — 输入值长度不能小于minlength表示的值
maxlength — 输入的值不能大于maxlength表示的值
min — 输入值不能小于min表示的值
max — 输入值不能大于max表示的值
与v-model同时使用
vue-validator会自动校验通过v-model动态设置的值。
<div id="app"> <validator name="validation"> <form novalidate> message: <input type="text" v-model="msg" v-validate:message="{required: ture, minlength: 8}" /> <br /> <p v-if="$validation.message.required">message不能为空</p> <p v-if="$validation.message.minlength">message输入太长位数</p> </form> </validator> </div> <script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script> <script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> var vm = new Vue({ el: '#app', data: { msg: '' } }); setTimeout(function () { vm.msg = 'hello world!'; }, 2000); </script>
重置校验结果
通过在Vue组件实例上调用$resetValidation();方法来动态重置校验结果。
<div id="app"> <validator name="validation"> <form novalidate> <div class="username-field"> <label for="username">username:</label> <input type="text" id="username" v-validate:username="['required']" /> </div> <div class="comment-filed"> <label for="comment">comment:</label> <input type="text" id="comment" v-validate:comment="{maxlength: 256}" /> </div> <div class="errors"> <p v-if="$validation.username.required">用户名不能为空</p> <p v-if="$validation.comment.maxlength">输入文字超过256个</p> <input type="submit" value="send" v-if="$validation.valid" /> <button type="button" @click="onReset">Reset Validation</button> </div> <pre>{{$validation | json}}</pre> </form> </validator> </div> <script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script> <script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: '#app', methods: { onReset: function () { this.$resetValidation(); } } }); </script>
复选框checkbox
<div id="app"> <validator name="validation"> <form novalidate> <h2>调查</h2> <fieldset> <legend>请选择水果</legend> <input type="checkbox" id="apple" value="apple" v-validate:fruits="{ required: { rule: true, message: requiredErrorMsg }, minlength: { rule: 1, message: minlengthErrorMsg }, maxlength: { rule: 2, message: maxlengthErrorMsg } }" /> <label for="apple">Apple</label> <input type="checkbox" id="orange" value="orange" v-validate:fruits /> <label for="orange">Orange</label> <input type="checkbox" id="grape" value="grape" v-validate:fruits /> <label for="grape">Grape</label> <input type="checkbox" id="banana" value="banana" v-validate:fruits /> <label for="banana">Banana</label> <ul class="errors"> <li v-for="msg in $validation.fruits"> <p>{{msg | json}}</p> </li> </ul> </fieldset> </form> </validator> </div> <script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script> <script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: '#app', computed: { requiredErrorMsg: function () { return '请选择水果'; }, minlengthErrorMsg: function () { return '请选择至少1个水果!'; }, maxlengthErrorMsg: function () { return '请选择最多2个水果!'; } } }); </script>
下拉类表select
<div id="app"> <validator name="validation"> <form novalidate> <select v-validate:lang="{required: true}"> <option value="">请选择语言</option> <option value="javascript">javascript</option> <option value="php">php</option> <option value="node">node</option> </select> <div class="errors"> <p v-if="$validation.lang.required">不能为空!</p> </div> </form> </validator> </div> <script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script> <script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: '#app' }); </script>
校验状态class
各校验状态都有其对应的class(默认) 也可以自定义校验状态class
<validator name="validation" :classes="{touched: 'touehc-validator', dirty: 'dirty-validator'}"> <label for="username">username</label> <<input type="text" id="username" :classes="{valid: 'valid-username', invalid: 'invalid-username'}" v-validate:username="{required: {rule: true, message: 'required you name!'}}"> </validator> <!-- classes属性只能在validator元素或应用了v-validate的元素上使用有效 -->
分组校验
vue-validator支持分组校验。例如重复密码功能。
<div id="app"> <validator name="validation" :groups="['passwordGroup']"> <form novalidate> username: <input type="text" v-validate:username="['required']" /><br /> password: <input type="password" v-validate:password="{ minlength: 8, required: true }" group="passwordGroup" /><br /> comfirm password: <input type="password" v-validate:password-comfirm="{minlength: 8, required: true}" group="passwordGroup" /> <div class="errors"> <p v-if="$validation.username.required">用户名不能为空</p> <p v-if="$validation.password.required">密码不能为空</p> <p v-if="$validation.password.minlength">密码不能少于8位</p> <p v-if="$validation.password-comfirm.required">重复密码不能为空</p> <p v-if="$validation.password-comfirm.minlength">密码不能少于8位</p> <p v-if="$validation.passwordGroup.valid">密码不一致</p> </div> </form> </validator> </div> <script src="//cdn.bootcss.com/vue/1.0.24/vue.js" type="text/javascript" charset="utf-8"></script> <script src="//cdn.bootcss.com/vue-validator/2.1.3/vue-validator.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: '#app' }); </script>
Vue是一套用于构建用户界面的渐进式JavaScript框架,Vue与其它大型框架的区别是,使用Vue可以自底向上逐层应用,其核心库只关注视图层,方便与第三方库和项目整合,且使用Vue可以采用单文件组件和Vue生态系统支持的库开发复杂的单页应用。
以上就是关于“Vue Validator表单验证插件怎么使用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。