<el-form-item label="库存数量" prop="stock">
<el-input oninput="value=value.replace(/[^\d]/g,'')" v-model="newly.stock" placeholder="请输入库存数量"></el-input>
</el-form-item>
<el-form-item label="单价">
<el-input v-model="newly.price" @input="limitNum(newly.price)" placeholder="请输入单价"/>
</el-form-item>
limitNum(amount) {
amount = amount
.replace(/[^\d.]/g, "") //只能输入数字
.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3") //只能输入两个小数
.replace(/\.{2,}/g, "."); //出现多个点时只保留第一个
// 第一位不让输小数点
if (amount == ".") {
amount = "";
}
// 如果第一位是0,第二位必须大于0或者小数点
if (amount.substring(0, 1) == 0) {
if (amount.substring(1, 2) > 0) {
amount = amount.substring(1, 2);
} else if (
amount.substring(1, 2) === 0 ||
amount.substring(1, 2) === "0"
) {
amount = "0";
}
} else {
// 如果第一位数字大于0(不等于0肯定就大于0),仅需考虑第二位是小数点的情况
if (amount.indexOf(".") !== -1) {
if (amount.substring(0, 1) > 0) {
console.log("第一位大于0");
} else {
console.log("第一位等于0");
if (amount.substring(2, 3) > 0) {
console.log("小数点后第一位大于0");
} else {
console.log("小数点后第一位等于0");
amount = "0.";
}
}
} else {
console.log("没有小数点,正常输入");
}
}
this.newly.price = amount;
},