天天看点

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('请输入正确数值!');
        }
      },