本文来源于网络资料整理,追求实用!
输入框输入类型限制比较常见,搜索资料良莠不齐,为了避免每次都查找、验证这一过程,特在此记录一下。
只能输入整数
去除前面多余的0
<el-input v-model="input" placeholder="请输入内容" oninput="value=value.replace(/\D/g,'').replace(/^0+(\d)/, '$1')"></el-input>
只能输入整数和小数
- 去除前面多余的0
- 只能输入一个小数点
<el-input v-model="input" placeholder="请输入内容" oninput="value=value.replace(/[^\d.]/g, '').replace(/^0+(\d)/, '$1').replace(/^\./, '0.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.').replace(/^\./g, '')"></el-input>
只能输入两位小数
- 去除前面多余的0
- 只能输入一个小数点
- 更改小数位数直接改match中的数字
<el-input v-model="input" placeholder="请输入内容" oninput="value=value.replace(/[^\d^\.]+/g, '').replace(/^0+(\d)/, '$1').replace(/^\./, '0.').match(/^\d*(\.?\d{0,2})/g)[0] || ''"></el-input>
可以为负值,只能输入四位小数
<el-input size="small" v-model.trim="scope.row.initValue" style="width: 80px;" oninput="value=value.replace(/[^\d^\.^\-]+/g, '').replace(/^0+(\d)/, '$1').replace(/^\./, '0.').replace(/^\-0+(\d)/, '-$1').replace(/^\-\./, '-0.').match(/((^\-?\d*)|(^\d*))(\.?\d{0,4})/g)[0] || ''"></el-input>
参考地址
- https://blog.csdn.net/weixin_44835297/article/details/111253150
- https://www.jianshu.com/p/1d88cceee153
- https://blog.csdn.net/weixin_44171757/article/details/102391297