溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

詳解vue表單驗(yàn)證組件 v-verify-plugin

發(fā)布時(shí)間:2020-09-20 12:30:46 來(lái)源:腳本之家 閱讀:143 作者:setfocus 欄目:web開(kāi)發(fā)

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()){
        //通過(guò)驗(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;
    }
  }
}

指令說(shuō)明

v-verify

v-erify 在表單控件元素上創(chuàng)建數(shù)據(jù)的驗(yàn)證規(guī)則,他會(huì)自動(dòng)匹配要驗(yàn)證的值以及驗(yàn)證的規(guī)則。

v-verify 修飾符說(shuō)明

該指令最后一個(gè)修飾符為自定義分組

//自定義teacher分組
v-verify.teacher
//自定義student分組
v-verify.student

//驗(yàn)證時(shí)可分開(kāi)進(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 錯(cuò)誤展示,當(dāng)有錯(cuò)誤時(shí)會(huì)展示,沒(méi)有錯(cuò)誤時(shí)會(huì)加上style:none,默認(rèn)會(huì)展示該數(shù)據(jù)所有錯(cuò)誤的第一條

該指令為語(yǔ)法糖(見(jiàn)示例)

<input v-model="username" v-verify="username">

<label v-show="$verify.$errors.username && $verify.$errors.username.length" v-text="$verify.$errors.username[0]"></label>
<!--等價(jià)于-->
<label v-verified="$verify.$errors.username"></label>
<!--展示所有錯(cuò)誤-->
<label v-verified.join="$verify.$errors.username">

修飾符說(shuō)明

.join 展示所有錯(cuò)誤 用逗號(hào)隔開(kāi)

自定義驗(yàn)證規(guī)則

var myRules={
  phone:{
    test:/^1[34578]\d{9}$/,
    message:"電話(huà)號(hào)碼格式不正確"
  },
  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
});

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI