詳解vue表單驗證組件 v-verify-plugin
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()){ //通過驗證 } } }, 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ù)的驗證規(guī)則,他會自動匹配要驗證的值以及驗證的規(guī)則。
v-verify 修飾符說明
該指令最后一個修飾符為自定義分組
//自定義teacher分組 v-verify.teacher //自定義student分組 v-verify.student //驗證時可分開進(jìn)行驗證 //驗證student 分組 this.$verify.check("student") //驗證teacher 分組 this.$verify.check("teacher") //驗證所有 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 展示所有錯誤 用逗號隔開
自定義驗證規(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í)有所幫助,也希望大家多多支持腳本之家。
- vue使用Element組件時v-for循環(huán)里的表單項驗證方法
- Vue2.0表單校驗組件vee-validate的使用詳解
- vue組件表單數(shù)據(jù)回顯驗證及提交的實例代碼
- Vue表單類的父子組件數(shù)據(jù)傳遞示例
- Vue form表單動態(tài)添加組件實戰(zhàn)案例
- vue動態(tài)綁定組件子父組件多表單驗證功能的實現(xiàn)代碼
- 使用form-create動態(tài)生成vue自定義組件和嵌套表單組件
- vue2.0數(shù)據(jù)雙向綁定與表單bootstrap+vue組件
- 利用Vue v-model實現(xiàn)一個自定義的表單組件
- vue懸浮表單復(fù)合組件開發(fā)詳解
相關(guān)文章
vue@cli3項目模板怎么使用public目錄下的靜態(tài)文件
這篇文章主要介紹了vue@cli3項目模板怎么使用public目錄下的靜態(tài)文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07vite+vue3中使用mock模擬數(shù)據(jù)問題
這篇文章主要介紹了vite+vue3中使用mock模擬數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03Object.assign觸發(fā)watch原理示例解析
這篇文章主要為大家介紹了Object.assign觸發(fā)watch原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11vue中如何動態(tài)綁定圖片,vue中通過data返回圖片路徑的方法
下面小編就為大家分享一篇vue中如何動態(tài)綁定圖片,vue中通過data返回圖片路徑的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02