最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解vue-validator(vue驗(yàn)證器)

 更新時(shí)間:2017年01月16日 14:17:31   作者:藝小晨  
本篇文章主要介紹了vue-validator(vue驗(yàn)證器),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

github項(xiàng)目地址:https://github.com/vuejs/vue-validator

單獨(dú)使用vue-validator的方法見官方文檔,本文結(jié)合vue-router使用。

安裝驗(yàn)證器

不添加自定義驗(yàn)證器或者無需全局使用的公用驗(yàn)證器,在main.js中安裝驗(yàn)證器,使用 CommonJS 模塊規(guī)范, 需要顯式的使用 Vue.use() 安裝驗(yàn)證器組件。

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

與 vue-router 同時(shí)使用,必須在調(diào)用 router#map, router#start 等實(shí)例方法前安裝驗(yàn)證。

若要自定義驗(yàn)證器,建一個(gè)js文件,在該文件中安裝驗(yàn)證器組件。例如:validation.js

import Vue from 'vue'
import Validator from 'vue-validator'
Vue.use(Validator)
//自定義驗(yàn)證器

自定義驗(yàn)證器

官方提供的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

但是以上的不一定滿足我們的需求,這時(shí)就需要用到另一個(gè)全局api,用于注冊和獲取全局驗(yàn)證器。

Vue.validator( id, [definition] )

示例  定義validation.js  內(nèi)容如下

import Vue from 'vue'
import Validator from 'vue-validator'
Vue.use(Validator)
//自定義驗(yàn)證器
//添加一個(gè)簡單的手機(jī)號驗(yàn)證 
//匹配0-9之間的數(shù)字,并且長度是11位
Vue.validator('tel', function (val) {
 return /^[0-9]{11}$/.test(val)
});
//添加一個(gè)密碼驗(yàn)證
//匹配6-20位的任何字類字符,包括下劃線。與“[A-Za-z0-9_]”等效。
Vue.validator('passw', function (val) {
 return /^(\w){6,20}$/.test(val)
});

使用驗(yàn)證器

驗(yàn)證器語法

<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個(gè)字符</span>
   <span v-show="$validation.comment.maxlength">不得大于15個(gè)字符</span>
  </div>
 </validator>

默認(rèn)情況下,vue-validator 會根據(jù) validator 和 v-validate 指令自動(dòng)進(jìn)行驗(yàn)證。然而有時(shí)候我們需要關(guān)閉自動(dòng)驗(yàn)證,在有需要時(shí)手動(dòng)觸發(fā)驗(yàn)證。如果你不需要自動(dòng)驗(yàn)證,可以通過 initial 屬性或 v-validate 驗(yàn)證規(guī)則來關(guān)閉自動(dòng)驗(yàn)證。如下:

<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個(gè)字符</span>
    <span v-show="$validation.comment.maxlength">不得大于15個(gè)字符</span>
   </div>
</validator>

Terminal 指令問題

<validator name="test_validator">
  <!-- @invalid:valid的逆 ,表示驗(yàn)證不通過 -->
  <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中導(dǎo)入 無需再次導(dǎo)入
//此處導(dǎo)入的是上面代碼的validation.js
import validator from '../validator/validation'
export default{
  data(){
    return{
      comment:'',
      passw:'',
      passw2:''
    }
  },
  methods:{
    passwInvalid(){
      alert('只能輸入6-20個(gè)字母、數(shù)字、下劃線');
    },
    passwok(){
      //alert('驗(yàn)證碼符合規(guī)范')
    }
  }
}
</script>

示例:用戶注冊驗(yàn)證

用了一個(gè)組件來顯示提示信息

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;
      }
    },
    //提示的內(nèi)容
    toasttext:{
      type:String,
      required: false,
      default:function(){
        return 'no message';
      }
    },
    //顯示的時(shí)間
    duration: {
      type: Number,
      default:3000,//默認(rèn)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>

注冊用戶:假如我們需要填寫手機(jī)號和輸入兩次密碼

<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='請輸入手機(jī)號碼'>
      </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>
//導(dǎo)入validation.js 此處的validation.js就是上文中validation.js的內(nèi)容
import validator from '../validator/validation';
//導(dǎo)入顯示提示信息的組件
import Toast from '../components/toast.vue';
export default{  
  components: {
    //注冊組件
     Toast
   },
  data(){
    return{
      telphone:'',//電話號碼
      toastshow:false,//默認(rèn)不現(xiàn)實(shí)提示信息
      toasttext:'',//提示信息內(nèi)容
      passw1:'',//首次輸入密碼
      passw2:''//再次輸入密碼
    }
  },
  methods:{
    //手機(jī)號驗(yàn)證失敗時(shí)執(zhí)行的方法
    telonInvalid(){
      //設(shè)置提示信息內(nèi)容
      this.$set('toasttext','手機(jī)不正確');
      //顯示提示信息組件
      this.$set('toastshow',true);
    },
    //密碼驗(yàn)證失敗時(shí)執(zhí)行的方法
    passwInvalid(){
      this.$set('toasttext','只能輸入6-20個(gè)字母、數(shù)字、下劃線');
      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) {
          //驗(yàn)證無效
           that.$set('toasttext','請完善表單');
           that.$set('toastshow',true);
        }else{
           that.$set('toasttext','驗(yàn)證通過');
           that.$set('toastshow',true);
           //驗(yàn)證通過做注冊請求
           /*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){
            //顯示返回的錯(cuò)誤信息
            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>

若點(diǎn)擊下一步,會提示“請完善表單”,因?yàn)轵?yàn)證不通過;若是文本框獲得焦點(diǎn)后失去焦點(diǎn)則會提示相應(yīng)的錯(cuò)誤信息;若內(nèi)容填寫正確,則會提示驗(yàn)證通過并發(fā)送相應(yīng)的請求。

效果如圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue v-for循環(huán)之@click點(diǎn)擊事件獲取元素示例

    Vue v-for循環(huán)之@click點(diǎn)擊事件獲取元素示例

    今天小編就為大家分享一篇Vue v-for循環(huán)之@click點(diǎn)擊事件獲取元素示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 使用vue ant design分頁以及表格分頁改為中文問題

    使用vue ant design分頁以及表格分頁改為中文問題

    這篇文章主要介紹了使用vue ant design分頁以及表格分頁改為中文問題,具有很好的參考價(jià)值,希望對大家有所幫助。
    2023-04-04
  • webpack安裝配置與常見使用過程詳解(結(jié)合vue)

    webpack安裝配置與常見使用過程詳解(結(jié)合vue)

    這篇文章主要介紹了webpack安裝配置與常見使用過程,主要結(jié)合vue實(shí)現(xiàn),通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • vue項(xiàng)目打包成桌面快捷方式(electron)的方法

    vue項(xiàng)目打包成桌面快捷方式(electron)的方法

    本文主要介紹了vue項(xiàng)目打包成桌面快捷方式(electron)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Vue監(jiān)聽Enter鍵的方法總結(jié)與區(qū)別

    Vue監(jiān)聽Enter鍵的方法總結(jié)與區(qū)別

    這篇文章主要給大家介紹了關(guān)于Vue監(jiān)聽Enter鍵的方法與區(qū)別的相關(guān)資料,在Vue中我們可以通過監(jiān)聽鍵盤事件來實(shí)現(xiàn)回車鍵切換焦點(diǎn)的功能,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-10-10
  • 關(guān)于Vite項(xiàng)目打包后瀏覽器兼容性問題的解決方案

    關(guān)于Vite項(xiàng)目打包后瀏覽器兼容性問題的解決方案

    本文主要介紹了關(guān)于Vite項(xiàng)目打包后瀏覽器兼容性問題的解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • VUE3頁面div如何點(diǎn)擊改變樣式

    VUE3頁面div如何點(diǎn)擊改變樣式

    這篇文章主要介紹了VUE3頁面div如何點(diǎn)擊改變樣式問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue跳轉(zhuǎn)外部鏈接始終有l(wèi)ocalhost的問題

    vue跳轉(zhuǎn)外部鏈接始終有l(wèi)ocalhost的問題

    這篇文章主要介紹了vue跳轉(zhuǎn)外部鏈接始終有l(wèi)ocalhost的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Vue2項(xiàng)目中Mock.js的完整集成與使用教程

    Vue2項(xiàng)目中Mock.js的完整集成與使用教程

    Mock.js 是一個(gè)可以在開發(fā)階段模擬后端數(shù)據(jù)接口的 JavaScript 庫,它能夠生成大量不同類型的隨機(jī)數(shù)據(jù),并且模擬真實(shí)的接口返回,允許前端開發(fā)在沒有真實(shí)后端接口的情況下進(jìn)行開發(fā),本文給大家介紹了Vue2項(xiàng)目中Mock.js的完整集成與使用教程,需要的朋友可以參考下
    2025-02-02
  • vue bus全局事件中心簡單Demo詳解

    vue bus全局事件中心簡單Demo詳解

    ue-bus 提供了一個(gè)全局事件中心,并將其注入每一個(gè)組件,你可以像使用內(nèi)置事件流一樣方便的使用全局事件。這篇文章給大家介紹了vue bus全局事件中心簡單Demo,需要的朋友參考下吧
    2018-02-02

最新評論

平武县| 丹巴县| 宾川县| 衡东县| 建湖县| 孟州市| 麦盖提县| 团风县| 玉屏| 津市市| 平顶山市| 综艺| 大名县| 鹤山市| 营山县| 晋宁县| 长寿区| 两当县| 共和县| 张北县| 定结县| 伊宁县| 廊坊市| 陆良县| 新邵县| 木里| 福州市| 定远县| 鄂尔多斯市| 克什克腾旗| 吉木萨尔县| 眉山市| 馆陶县| 朝阳县| 沙洋县| 石景山区| 江永县| 庆阳市| 和顺县| 衡阳市| 丹巴县|