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
    })

 

           function numberFormat(value, decimals) {
                value = parseFloat(value)
                decimals = decimals != null ? decimals : 2;
                if (!isFinite(value) || !value && value !== 0) return '';
                return value.toFixed(decimals)
            }

 

var price = this.price
price = price.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
price = price.replace(/^\./g, ""); //验证第一个字符是数字
price = price.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
price = price.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
price = price.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
if (price.indexOf(".") < 0 && price != "") { //以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
    price = parseFloat(price);
}
this.price = price

 

posted @ 2020-03-13 15:37  justsilky  阅读(180)  评论(0编辑  收藏  举报