input 只能输入价格

<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)
      })
    },
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容