输入框只能输入数字遇到的坑 只能输入数字、小数及负数

输入框只能输入非负整数,数字首位不能一个或者多个0,单个0除外

方案一:

// 对应数值输入时校验
onInput() {
    // 清空数字以外的字符
    this.value = this.value.replace(/\D/g, '')
    // 转成数字
    this.value = Number(this.value)
}

方案二:

// 对应数值输入时校验
onInput() {
    // 清空数字以外的字符
    this.value = this.value.replace(/\D/g, '')
    // 清除数字前面的0,只有一位0除外
    this.value = this.value.replace(/0*(\d+)/,'$1')
}

输入框只能输入非负数,可以是小数,数字首位不能一个或者多个0,单个0除外

      this.costPrice = this.costPrice.replace(/[^\d.]/g, '');
      //必须保证第一个为数字而不是.
      this.costPrice = this.costPrice.replace(/^\./g, '');
      //保证只有出现一个.而没有多个.
      this.form.costPrice = this.costPrice.replace(/\.{2,}/g, '.');
      //保证.只出现一次,而不能出现两次以上
      this.costPrice = this.costPrice.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
      // 清除数字前面的0,只有一位0除外
      this.costPrice = this.costPrice.replace(/0*(\d+)/, '$1');

不知道,还有没有简便的方法。我现在用这个好像还行

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值