限制input只能输入数字且限制只能输入两位小数


  • chrome中的input不要加type=”number”,有问题,输入负号截取到第一个字符串是空,就默认text就行
  • 若允许负数加上allowMinus类,正数只加limitNumber
  • 以下可以封装成函数,加到DOM上的οnkeyup=foo(this)
    $(document).on('keyup', '.limitNumber,.allowMinus', function (e) {
      //修复第一个字符是小数点 的情况. 
      let fa = ''
      if (this.classList.contains('allowMinus')) { //或者$(this).hasClass('allowMinus')
        this.value.substring(0, 1) === '-' && (fa = '-')
      }
      if (this.value !== '' && this.value.substr(0, 1) === '.') {
        this.value = "";
      }
      this.value = this.value.replace(/^0*(0\.|[1-9])/, '$1');//解决 粘贴不生效
      console.log(this.value)
      this.value = this.value.replace(/[^\d.]/g, "");  //清除“数字”和“.”以外的字符
      this.value = this.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
      this.value = this.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
      this.value = this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');//只能输入两个小数
      if (this.value.indexOf(".") < 0 && this.value !== "") {//以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
        if (this.value.substr(0, 1) === '0' && this.value.length === 2) {
          this.value = this.value.substr(1, this.value.length);
        }
      }
      this.value = fa + this.value
    })

以下是自己写的方法,比较呆,已放弃, 采用上述方法

$(document).on('keydown', '.limitNumber,.allowMinus', function (e) {
    //8 = backspace;45=insert;46=delete;189 = '-';110小键盘.
      if(this.classList.contains('allowMinus')){
        if(e.keyCode === 189){
          return false
        }
      }
    if (e.keyCode !== 110 && e.keyCode !== 190 && e.keyCode !== 8 && e.keyCode !== 46 && e.keyCode !== 45 && (e.keyCode < 48
        || (e.keyCode > 57 && e.keyCode < 96) || e.keyCode > 110)) {
      return false
    }
  })
    .on('keyup', '.limitNumber', function (e) {
      let fa = ''
      if(this.classList.contains('allowMinus')){
        this.value.substring(0, 1) === '-' && (fa = '-')
      }
      this.value = this.value.replace(/[^\x00-\x80]/gi, '')
      //[^0-9.] 匹配不是数字和小数点的字符
      //[.][0-9]*[.] 匹配第一个小数点之后的数字,若多写小数点则归位到第一个小数点位置
      let str = (this.value.replace(/[^0-9.]/g, '')).replace(/.][0-9]*[.]/, '.').replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')
      str.substring(0, 1) === '.' && (str = '0' + str)
      this.value = fa + str
    })
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值