input只能输入数字以及小数点后两位

本文介绍了一种在前端开发中实现金额输入验证的方法,通过监听输入框的变化并使用正则表达式过滤非法字符,确保金额格式正确。同时,对金额进行格式化处理,避免重复小数点、前导零等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    watch: {
            'query.amount' (n, o) {
                this.$nextTick(() => {
                  this.trimQuery('amount')
                    this.filterHandle(/[^\d.]/g, 'amount')
                  // this.filterHandle(/\.{2,}/g, 'amount')
                  // this.filterHandle(/^\./g, 'amount')
                  // this.filterHandle(/^[0]{2,}/g, 'amount')
                  //  this.filterHandle(/^[0]\d/g, 'amount')
                  // this.filterHandle(/\.\d{3,}/g, 'amount')
                      this.filterHandle3(/\.{2,}/g, 'amount')
                    this.filterHandle3(/^\./g, 'amount')
                    this.filterHandle3(/^[0]{2,}/g, 'amount')
                      this.filterHandle3(/^[0]\d/g, 'amount')
                    this.filterHandle3(/\.\d{3,}/g, 'amount')
                })
            },
        },
  filterHandle3 (reg, prop) {
               if(reg.test(this.query[prop])){
                 let str = this.query[prop].slice(0,-1)
                 this.$set(this.query, prop,str)
               }
              
            },
             filterHandle (re, prop) {
              this.$set(this.query, prop, this.query[prop].replace(re, ''))
            },
 trimQuery (prop) {
              this.$set(this.query, prop, this.query[prop].trim())
            },

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值