VUE 数量、单价输入框

<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;
        },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值