天天看點

input框限制隻能輸入數字,不能輸入小數

html代碼

<el-input clearable v-model="randomInput"
                @input.native="changeNum(randomInput, 'randomInput')" @blur="randomInputBlur">
              </el-input>
           

js方法

changeNum(val, nodeType) {
        this.$nextTick(() => {
          if (this[nodeType] !== null) {
            this[nodeType] = this[nodeType].replace(/[^\d]/g, '')
          }
        })
      },
           

輸入框失焦提示

// 随機抽取份輸入框失焦
      randomInputBlur() {
        if (this.randomInput <= 0 && this.randomInput != '') {
          this.randomInput = '';
          return this.$message.error('請輸入正确數值!');
        }
        if (!this.randomInput.match(/(^[\-0-9][0-9]*(.[0-9]+)?)$/) && this.randomInput != '' && this
          .randomRadio == '1') {
          this.randomInput = '';
          return this.$message.error('請輸入正确數值!');
        }
      },