zoukankan      html  css  js  c++  java
  • 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+).(dd).*$/, '$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)
                }
  • 相关阅读:
    将vs2010换成vs2012的主题
    写了个油猴脚本
    Myeclipse10下的access数据库配置
    点击按钮触发声音(xaml实现)
    自定义的可拖动窗体
    在博客添加时钟
    定时器写的闪光字
    C语言I博客作业02
    c语言|博客作业02
    关于软件工程的一些疑问
  • 原文地址:https://www.cnblogs.com/justsilky/p/12486870.html
Copyright © 2011-2022 走看看