verify
github:https://github.com/liuyinglong/verify
npm:https://www.npmjs.com/package/vue-verify-plugin
install
npm install vue-verify-plugin
use
html
<div> <div> <input type="text" placeholder="姓名" v-verify.grow1="username" v-model="username"/> <label v-verified="verifyError.username"></label> </div> <div> <input type="password" placeholder="密碼" v-verify.grow1="pwd" v-model="pwd"/> <label v-verified="verifyError.pwd"></label> </div> <button v-on:click="submit">確認(rèn)</button> </div>
js
import Vue from "vue";import verify from "vue-verify-plugin";Vue.use(verify);export default{ data:function(){ return { username:"", pwd:"" } }, methods:{ submit:function(){ if(this.$verify.check()){ //通過驗(yàn)證 } } }, verify:{ username:[ "required", { test:function(val){ if(val.length<2){ return false; } return true; }, message:"姓名不得小于2位" } ], pwd:"required" }, computed:{ verifyError:function(){ return this.$verify.$errors; } }}指令說明
v-verify
v-erify 在表單控件元素上創(chuàng)建數(shù)據(jù)的驗(yàn)證規(guī)則,他會自動匹配要驗(yàn)證的值以及驗(yàn)證的規(guī)則。
v-verify 修飾符說明
該指令最后一個修飾符為自定義分組
//自定義teacher分組v-verify.teacher//自定義student分組v-verify.student//驗(yàn)證時可分開進(jìn)行驗(yàn)證 //驗(yàn)證student 分組this.$verify.check("student")//驗(yàn)證teacher 分組this.$verify.check("teacher")//驗(yàn)證所有this.$verify.check();v-verified
v-verified 錯誤展示,當(dāng)有錯誤時會展示,沒有錯誤時會加上style:none,默認(rèn)會展示該數(shù)據(jù)所有錯誤的第一條
該指令為語法糖(見示例)
<input v-model="username" v-verify="username"><label v-show="$verify.$errors.username && $verify.$errors.username.length" v-text="$verify.$errors.username[0]"></label><!--等價于--><label v-verified="$verify.$errors.username"></label><!--展示所有錯誤--><label v-verified.join="$verify.$errors.username">
修飾符說明
.join 展示所有錯誤 用逗號隔開
自定義驗(yàn)證規(guī)則
var myRules={ phone:{ test:/^1[34578]/d{9}$/, message:"電話號碼格式不正確" }, max6:{ test:function(val){ if(val.length>6) { return false } return true; }, message:"最大為6位" }}import Vue from "vue";import verify from "vue-verify-plugin";Vue.use(verify,{ rules:myRules});以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答