JS通过正则限制input输入框只能输入数字

3,529 阅读1分钟

vue中

<el-input placeholder="请输入" v-model="seavalue" clearable style="width: 200px;" @input="getinput"></el-input>
//限制只能输入数字及小于11位
    getinput(value) {
      let that = this
      value = value.length > 11 ? value.slice(0, 11) : value
      that.seavalue = value.replace(/[^\d]/g, '');
    },

原生或jquery中

<input type="number" onKeyUp="value=value.replace(/[^\d]/g,'')">

用到的正则含义

/[^\d]/g //去除字符串中除了数字以外的其他字符
/[^\.\d]/g //去除字符串中除了数字和点以外的其他字符