正则限制input输入小数点后保留两位小数

<div><input type="text" v-model="newPrice" /></div>

export default {

data() {

    return { newPrice: "",  timer: null };

},
watch: {
    newPrice(val){
        clearTimeout(this.timer); //防抖

        this.timer = setTimeout(() => {
              /**
              *小数点不好控制,把控不了用户输入后是否继续输入,
              *所以如果输入后1秒内没有再输入则小数点就会被清掉
              */

              let reg = /^(0|[1-9]\d*)(\.\d{1,2})?/;

              let price = val.match(reg);

              this.newPrice = price ? price[0] : '';

         }, 1000);
    }
}

}

猜你喜欢

转载自blog.csdn.net/Cris_are/article/details/107311482