只能输入数字
this.inputValue = this.inputValue.replace(/[^\d]/g, "");
只能输入数字和小数点
this.inputValue = this.inputValue.replace(/[^\d.]/g, "");
限制小数点输入个数,只能输入一个小数点
this.inputValue = this.inputValue.replace(/\.{2,}/g, ".");
this.inputValue = this.inputValue
.replace(".", "$#$")
.replace(/\./g, "")
.replace("$#$", ".");
只能输入一位小数点
this.inputValue = this.inputValue.replace(
/^(-)*(\d+)\.(\d).*$/,
"$1$2.$3"
);
只能输入两位小数点
this.inputValue = this.inputValue.replace(
/^(-)*(\d+)\.(\d\d).*$/,
"$1$2.$3"
);
只能输入三位小数点,之后的以此类推
this.inputValue = this.inputValue.replace(
/^(-)*(\d+)\.(\d\d\d).*$/,
"$1$2.$3"
);
如果有小数点是可以输入0.1,0.001等,但是没有小数点就不能输入0开头的数字,即不能输入01,02等。
if (
this.inputValue &&
this.inputValue.indexOf(".") < 0 &&
this.inputValue != ""
) {
this.inputValue = parseFloat(this.inputValue);
this.inputValue = this.inputValue + "";
}
当有小数点位数限制时,例如小数点后可以输入六位小数
if (
this.inputValue.indexOf(".") > 0 &&
this.inputValue.length - this.inputValue.indexOf(".") > 6
) {
let str = this.inputValue.slice(
this.inputValue.indexOf("."),
this.inputValue.length
);
if (str / 1 <= 0) {
this.inputValue = this.inputValue.replace(str, "");
}
}
在 blur 事件中,如果小数点后面全是零,则清空小数点和后面的零
if (this.inputValue.indexOf(".") > 0) {
let str = this.inputValue.slice(
this.inputValue.indexOf("."),
this.inputValue.length
);
if (str / 1 <= 0) {
this.inputValue = this.inputValue.replace(str, "");
}
}