element-ui input只能输入正负数保留2位小数

正确值例如:0.01,-0.01,-1.50,-1.05,100.52,-100.52,100.00等

错误值过滤:-,a-z,01,-.,-,-0,01,0500,-.12,0.333等

实现方法:

<el-input v-if="state == 1" v-model="scope.row.materielExtraCost" placeholder="" size="small" @keyup.native="scope.row.materielExtraCost=scope.row.materielExtraCost.replace(scope.row.materielExtraCost,RestrictedMoney(scope.row.materielExtraCost))" @change="materielExtraCostChange(scope.row)"/>

// 额外费用校验输入正负数, 保留2位小数 调用公共方法
RestrictedMoney(values) {
  return plusOrMinus(values.toString())
},
// 结合change事件对失去焦点进行判断,防止输入一些无效值
    materielExtraCostChange(item) {
      // 防止删除为空
      if (!item.num) {
        item.num = '0.00'
      }
      // 一些错误金额输入的判断
      if (item.materielExtraCost.toString().indexOf('.') > 0 && Number(item.materielExtraCost.toString().split('.')[1].length) < 1) {
        item.materielExtraCost = item.materielExtraCost.toString().split('.')[0]
      }
      // 一些错误金额输入的判断
      if (!item.materielExtraCost || item.materielExtraCost === '-' || item.materielExtraCost === '-0') {
        item.materielExtraCost = '0.00'
        return
      }
      item.materielExtraCost = parseFloat(item.materielExtraCost).toFixed(2)
      // 此时item.materielExtraCost的值即是最终有效值
      // .......在这里可以继续你的代码..........
    },

// 公共方法部分
/* 校验输入正负数, 保留2位小数 传来的需要是string类型*/
export function plusOrMinus(values) {
  let newValue
  if (!(/[^0-9.-]/g.test(values))) {
    newValue = values.replace(/[^\-\d.]/g, '').replace(/\b(0+){2,}/g, '0').replace(/\-{2,}/g, '-').replace(/^\./g, '').replace(/\.{2,}/g, '.').replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
    if (newValue.toString().indexOf('.') > 0 && Number(newValue.toString().split('.')[1].length) > 2) {
      newValue = parseInt(parseFloat(newValue) * 100) / 100
    }
    if ((newValue.toString().split('-').length - 1) > 1) {
      newValue = parseFloat(newValue) || ''
    }
    if ((newValue.toString().split('-').length) > 1 && newValue.toString().split('-')[0].length > 0) {
      newValue = parseFloat(newValue) || ''
    }
    if (newValue.toString().length > 1 && (newValue.toString().charAt(0) === '0' || (newValue.toString().length > 2 && newValue.toString().charAt(0) === '-' && newValue.toString().charAt(1) === '0' && newValue.toString().charAt(2) !== '.')) && newValue.toString().indexOf('.') < 1) {
      newValue = parseFloat(newValue) || ''
    }
    // 判断整数位最多为9位
    if (newValue.toString().indexOf('.') > 0 && Number(newValue.toString().split('.')[0].length) > 9) {
      newValue = newValue.toString().substring(0, 9) + '.' + newValue.toString().split('.')[1]
    } else if (newValue.toString().indexOf('.') < 0 && Number(newValue.toString().split('.')[0].length) > 9) {
      newValue = newValue.toString().substring(0, 9)
    }
  } else {
    newValue = values.replace(/[^0-9.-]/g, '')
  }
  return newValue
}

如果对您有帮助,可以点赞支持下哦!

如果有更简单的方式实现,欢迎私信我,相互学习~

转载于:https://my.oschina.net/lemonfive/blog/3042415

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值