我们设置type为number时确实可以防止字母或者其他字符的输入,但是e这个字母其实在数学上是一个无限不循环小数来的,所以我们设置输入框为number时也是可以输。而且还有可能输入02.1或者1.2.3这种情况,对于一些金额或者其他的需求,这种格式其实不是很好。
将type改为text,通过正则表达式过滤让el-input只能输入正数
<el-input
type="text"
size="mini"
v-model.trim="value"
placeholder="请输入"
@input="inputChange(value)"></el-input>
inputChange(value){
//防止其他字符的输入
this.value =value.replace(/[^\d.]/g, "");
//清除“数字”和“.”以外的字符
this.value = this.value.replace(/\.{2,}/g, ".");
//只保留第一个. 清除多余的
this.value = this.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
if (this.value.indexOf(".") < 0 && this.value != "") {
//此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
this.value = parseFloat(this.value);
}
},