vue--自定義驗證指令


參考文檔:

https://cn.vuejs.org/v2/guide/custom-directive.html
https://www.cnblogs.com/ilovexiaoming/p/6840383.html

實例代碼:

<template>
  <div id="app" class="app">
  <h3>{{msg}}</h3>
  <div class="input">
    <input type="text" v-input v-focus ><span>{{msg1}}</span>
  </div>
  <div class="input">
    <input type="text" v-input v-required ><span>{{msg2}}</span>
  </div>
  <div class="input">
    <!-- required:true/false 表示這個是必填項 -->
    <input type="text" v-input v-checked="{required:true,}" ><span>{{msg3}}</span>
  </div>
  <div class="input">
    <!-- <input type="text" v-input v-validate="'required|min(6)|max(3)|minlength(2)|minlength(10)|regex([0~6])'"> -->
    <input type="text" v-input v-validate="'required|min(2)|max(20)|minlength(6)|maxlength(12)'"><span>{{tipsMsg}}</span>
  </div>
  </div>
</template>

<script>
export default {
  name: 'App',
  data () {
    return {
      msg: '指令',
      tipsBorderColor:'red',
      msg1: '最簡單的指令',
      msg2: '驗證不能為空的指令',
      msg3: '進行正則驗證',
      tipsMsg:'',
    }
  },
  directives: {
    // 修飾input框的指令
    input: {
      // 當被綁定的元素插入到DOM上的時候
      inserted: function(el){
        el.style.width = "300px";
        el.style.height = "35px";
        el.style.lineHeight = "35px";
        el.style.background = "#ddd";
        el.style.fontSize = "16px";
        el.style.border = "1px solid #eee";
        el.style.textIndent = "5px";
        el.style.textIndent = "8px";
        el.style.borderRadius = "5px";
      }
    },
    // input框默認選中的指令
    focus:{
      inserted:function(el){
        el.focus();
      }
    },
    // 不能為空的指令
    required:{
      inserted: function(el){
        el.addEventListener('blur',function(event){
          if(el.value == '' || el.value == null){
            el.style.border = "1px solid red";
            console.log('我不能為空');
          };
        });
      }
    },
    // 驗證指令
    checked:{
      inserted: function(el){

      }
    },
    // 驗證
    validate: {
      inserted:function(el,validateStr){
        // 將驗證規則拆分為驗證數組
        let validateRuleArr = validateStr.value.split("|");
        console.log(validateStr);
        console.log(validateRuleArr);
        // 監聽失去焦點的時候
        el.addEventListener('blur',function(event){
          console.log(typeof(el.value));
          //失去焦點進行驗證
          checkedfun();
        });
        // 循環進行驗證
        function checkedfun(){
          for(var i=0; i<validateRuleArr.length; ++i){
            let requiredRegex = /^required$/; // 判斷設置了required
            let minRegex = /min\(/; //判斷設置了min 最小值
            let maxRegex = /max\(/; //判斷設置了max 最大值
            let minlengthRegex = /minlength\(/; //判斷設置了 minlength 最大長度
            let maxlengthRegex = /maxlength\(/; //判斷設置了 maxlength 最大長度
            // 判斷是否是 required 調用 require的方法
            if(requiredRegex.test(validateRuleArr[i])){
              if(!required()){break;};
            };
            // 判斷是否設置了最小值
            if(minRegex.test(validateRuleArr[i])){
              if(!eval(validateRuleArr[i])){break;};
            };
            // 判斷是否設置了最大值
            if(maxRegex.test(validateRuleArr[i])){
              if(!eval(validateRuleArr[i])){break;};
            };
            // 判斷設置了最小長度
            if(minlengthRegex.test(validateRuleArr[i])){
              if(!eval(validateRuleArr[i])){break;};
            };
            // 判斷設置了最大長度
            if(maxlengthRegex.test(validateRuleArr[i])){
              if(!eval(validateRuleArr[i])){break;};
            };
          };
        }
        // 驗證是否是必填項
        function required(){
          if(el.value == '' || el.value == null){
            console.log("不能為空");
            return false;
          };
          return true;
        }
        // 最小值驗證
        function min(num){
          if(el.value < num){
            console.log('最小值不能小於'+num);
            return false;
          };
          return true;
        };
        // 最大值驗證
        function max(num){
          if(el.value > num){
            console.log('最大值不能大於'+num);
            return false;
          };
          return true;
        };
        // 最小長度驗證
        function minlength(length){
          if(el.value.length < length){
            console.log('最小長度不能小於'+length);
            return false;
          };
          return true;
        };
        // 最大長度進行驗證
        function maxlength(length){
          if(el.value.length > length){
            console.log('最大長度不能大於'+length);
            return false;
          };
          return true;
        }
      }
    }
  }
}
</script>

<style>
#app{}
.input{padding-bottom:20px;}
.app input{width: 300px; height: 35px; outline:none; background:#ddd;}
.app span{padding-left:20px;}
</style>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM