Bootstrap

elementUI el-input 只能输入正整数验证

字母e在js中属于数字,但是正则匹配 \d 是拦不住字母e 的

正确写法为:

onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode || event.which))) || event.which === 8"
<el-input type="number" placeholder="请输入" min="1" onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode || event.which))) || event.which === 8" v-model.number="count"></el-input>

其他写法: 

// 只允许输入数字

<el-input type="number" placeholder="请输入" min="1" oninput ="value=value.replace(/[^\d]/g,'')"  v-model.number="count"></el-input>

同理,只允许输入数字和小数

oninput ="value=value.replace(/[^0-9.]/g,'')"

 允许输入小数点后几位:

// 保留一位小数
oninput="if(isNaN(value)) { value = parseFloat(value) } if(value.indexOf('.')>0){value=value.slice(0,value.indexOf('.')+2)}" 

// 若需要保留N位小数,则把2 改为 1 + n即可

设置范围,最大值,最小值,监听input 及 change事件

<el-input min="0" max="100" type="number" @input="numberChange($event, 100)" @change="numberChange($event, 100)" onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode || event.which))) || event.which === 8" v-model.number="count"></el-input> 
numberChange (val, max) {
      this.$nextTick(() => {
        this.count = Math.min(parseInt(val), max)
      })
    }

 

;