天天看點

input數字輸入框,自定義保留小數位數,禁止輸入多個小數點,以及對非數字字元的處理

input數字輸入框,自定義保留小數位數,禁止輸入多個小數點,以及對非數字字元的處理

<template>
  <input class="numberInput"  type="number" :placeholder="placeholder" v-model="inputData" @keydown="keydownFn" />
</template>
<script>
  export default {
    props: {
      point: {
        default: 0
      },
      max: Number,
      placeholder: String,
      value: {
        default: null
      },
    },
    computed: {
      inputData: {
        get:function() {
          return this.value;
        },
        set:function (value) {
          let val = this.$el.value;
          let len = val.length;

          // 若輸入08,變為8
          if(len==2 && val.charAt(0)==0 && val.charAt(1)!='.'){
            this.$el.value = val.charAt(1);
            this.setParentModeVal(this.$el.value);
            return;
          }

          // 禁止輸入多個小數點
          if(Math.abs(this.value) > 0 && val==='' && value ===''){
            if(this.keyDownDel){
              this.$el.value = '';  
            }else {
              this.$el.value = this.value;  
            }
            this.setParentModeVal(this.$el.value);
            return ;
          }

          // 處理輸入“點、e、+、-等被識别為數字字元
          if(this.value === '' && val === '' && value === ''){  //字母類非數字輸入都為'';
            this.$el.value = '';
            this.setParentModeVal('');
            return ;
          }

          // 保留小數
          if(val){
            let pointIndex = val.indexOf('.');
            if(this.point==0 && pointIndex!== -1){
              this.$el.value = val.substr(0, pointIndex);
              this.setParentModeVal(this.$el.value);
              return ;
            }
            if(pointIndex>0 && (len - pointIndex)>(this.point+1)){
              this.$el.value = val.substr(0,pointIndex + this.point +1);
              this.setParentModeVal(this.$el.value);
              return ;
            }
          }

          // 最大值
          if(this.max>0 && val>this.max){
            this.$el.value = val.substr(0,len-1);
            this.setParentModeVal(this.$el.value);
            return;
          }

          this.setParentModeVal(val);
          return;
        }
      }
    },
    data() {
      return {
        keyDownDel: false,// 判斷鍵盤輸入值
      }
    },
    methods: {
      setParentModeVal(value){
        this.$emit('input', value);
      },
      keydownFn(event){
        let e = event || window.event;
        if(e.keyCode==8||e.keyCode==46){   //Backspace || Delete
          this.keyDownDel = true;
        }else {
          this.keyDownDel = false;
        }
      }
    },
  }
</script>

<style  scoped>
  .numberInput {
    width: 100%;
    vertical-align: middle;
    border-radius: 4px;
    border: 1px solid #dcdee2;   //變換邊框樣式
    padding: 4px 7px;
    height: 32px;
    color: #515a6e;
    outline:medium;  //去除預設滑鼠點選邊框樣式
  }
  .numberInput:hover {
    border-color: #f75b46;
  }
  .numberInput:focus {
    border-color: #f75b46;
    outline: 0;
    box-shadow: 0 0 0 2px rgba(245, 50, 24, .2);
  }
  input::-webkit-input-placeholder {
    color: #c5c8ce;
  }
  // input::-webkit-inner-spin-button,   //去掉右側加減的小箭頭
  // input::-webkit-outer-spin-button {
  //   -webkit-appearance: none;
  //   margin: 0;
  // }
</style>

           

引入代碼:

import numberInput from '~/components/common/numberInput';
           
<numberInput :point="2" :min="0" placeholder="請輸入金額" v-model="formData.amount"></numberInput>