只能输入数字且只能两位小数的input框验证

//只能输入数字且只能两位小数
    Vue.prototype.$check_decimal = function(decimal){
      var inspect_decimal = '' + decimal;
          inspect_decimal = inspect_decimal
          // .replace(/[^\d.]/g,'').replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')
        .replace(/[^\d.]/g, '') // 清除“数字”和“.”以外的字符
        .replace(/\.{2,}/g, '.') // 只保留第一个. 清除多余的
        .replace('.', '$#$')
        .replace(/\./g, '')
        .replace('$#$', '.')
        .replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); // 只能输入两个小数
        // .replace(/^[0-9]{6}$/i,'')

        // 以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
        if (inspect_decimal.indexOf('.') < 0 && inspect_decimal != '') {
           // if (temp.length <= 5) {
           //      return;//小于五位数直接返回
           //  } else {
           //      // val.value.delete(5, 6);//大于五位数就删掉第六位(只会保留五位)
           //      if (temp.length > 5) {
           //          val = temp.substring(1, 5 + 1);
           //      }
           //      return inspect_decimal;
           //  }
            inspect_decimal = parseFloat(inspect_decimal);
        }
        decimal = inspect_decimal
        return decimal
    }

在页面中使用

checkNumber(attribute) {
      this.addForm[attribute] = this.$check_decimal(this.addForm[attribute]);
    }

你可能感兴趣的:(解bug)