vue 限制 input 只能输入正负数并最多2位小数

本文介绍了如何使用Vue来限制input输入,只允许用户输入正负数且最多保留两位小数。通过饿了么UI组件库的input组件,结合事件监听和自定义方法进行输入值的过滤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先给饿了么input 绑上方法

                <el-input   v-model="educationReserve.monthMoney" placeholder="" size="small"
                            @input.native="educationReserve.monthMoney=
                            educationReserve.monthMoney.replace
                            (educationReserve.monthMoney,RestrictedMoney
                            (educationReserve.monthMoney))"
                            @change="materielExtraCostChange(educationReserve.monthMoney)"/>

通过监听输入走一个公共方法过滤:

plusOrMinus(values) {
      let newValue;
      if (!(/[^0-9.-]/g.test(values))) {
        newValue = values.replace(/[^\-\d.]/g, '').replace(/\-{2,}/g, '-').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;
    },

下面 2 个是input方法:

  // 额外费用校验输入正负数, 保留2位小数 调用公共方法
    RestrictedMoney(values) {
      return this.plusOrMinus(values.toString());
    },
    // 结合change事件对失去焦点进行判断,防止输入一些无效值
    materielExtraCostChange(item) {
      // 防止删除为空
      if (!item) {
        item = '0.00';
      }
      // 一些错误金额输入的判断
      if (item.toString().indexOf('.') > 0 && Number(item.toString().split('.')[1].length) < 1) {
        item = item.toString().split('.')[0];
      }
      // 一些错误金额输入的判断
      if (!item || item === '-' || item === '-0') {
        item = '0.00';
        return;
      }
      item = parseFloat(item).toFixed(2);
      this.educationReserve.monthMoney = item ;
    },

现在 input 只能输入 1.11 、 -1.11、1

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值