美文网首页程序员
input 只能输入价格

input 只能输入价格

作者: Amituofo_ | 来源:发表于2021-02-03 16:48 被阅读0次
<el-input  v-model="unitPrice"  placeholder="Text Input" @input="priceFormat"></el-input>
priceFormat (e) {
      this.unitPrice = (this.unitPrice.match(/^\d*(\.?\d{0,2})/g)[0]) || null
      if (isNaN(this.unitPrice)) {
        this.unitPrice = ''
      }
     // 在不是“0.”开头的字符进行修改:“01”=>1
      let price = e.toString()
      if (price.charAt(0) == "0" && price.charAt(1) != "." && this.unitPrice.length >= 2) {
        this.unitPrice = this.unitPrice.replace(/0/, "")
      }
}

 handleAmountChange (e) {
      let unitPrice = null
      //过滤e字符
      e.target.value = e.target.value.replace("e", "");

      //判断输入是数字和.
      e.target.value = e.target.value.replace(/[^\d.]/g, "");
      // 保证.只出现一次,而不能出现两次以上
      e.target.value = e.target.value
        .replace(".", "$#$")
        .replace(/\./g, "")
        .replace("$#$", ".");

      unitPrice = e.target.value;

      // 必须保证第一个为数字而不是.
      unitPrice = unitPrice.replace(/^\./g, "0.");

      // 保证只有出现一个.而没有多个.
      unitPrice = unitPrice.replace(/\.{2,}/g, ".");

      //只能输入两个小数
      // this.unitPrice = this.unitPrice.replace(/^()*(\d+)\.(\d\d).*$/, "$1$2.$3");

      this.$nextTick(() => {
        this.unitPrice = unitPrice
        console.log(this.unitPrice)
      })
    },

相关文章

网友评论

    本文标题:input 只能输入价格

    本文链接:https://www.haomeiwen.com/subject/bbkmtltx.html