自增input框的一些校验

自增input框的一些校验

结构:一行为两个input框,左侧为充值金额,右侧为获得金额。
校验规则:充值金额不能有重复,并且同一行的获得金额不得大于充值金额。

const benefitvValidator = (rule, value, callback) => {
    // 获得金额校验
    const id = rule.field.split('.')[1]
    const value1 = field.getValue(`principal.${id}`)
    if (!value) {
      field.setError(`realAmount.${id}`, '')
      setTimeout(() => {
        field.setError(`realAmount.${id}`, '请输入获得金额!')
      }, 0)
      return callback()
    } else {
      if (Number(value) < Number(value1)) {
        setTimeout(() => {
          field.setError(`realAmount.${id}`, '获得金额需大于充值金额!')
        }, 0)
      }
      const tempList = `${value}`.split('.')
      if (value > 100000) {
        return callback('充值金额最大为100000元!')
      } else if (tempList[1] && tempList[1].length > 2) {
        return callback('最多支持两位小数!')
      } else {
        return callback()
      }
    }
  }

                    <Input
                      addonTextAfter="元"
                      style={{ width: '160px' }}
                      name={`principal.${x.id}`}
                      defaultValue={x.principalYuan}
                      disabled={x.principal ? true : false}
                      onChange={() => {
                        // 横向校验 realAmount
                        const value1 = field.getValue(`principal.${x.id}`)
                        const value2 = field.getValue(`realAmount.${x.id}`)
                        if (Number(value2) < Number(value1) && value2 !== '') {
                          field.setError(
                            `realAmount.${x.id}`,
                            '获得金额需大于充值金额!'
                          )
                        } else {
                          field.setError(`realAmount.${x.id}`, '')
                        }

                        // 纵向校验
                        const values = field.getValues()
                        const arr = []
                        const arr2 = []
                        const temp = {}
                        const keys = Object.keys(values).filter(key =>
                          key.includes('principal')
                        )
                        keys.forEach(key => {
                          const val = Arithmetic.yuanToCent(values[key])
                          console.log(val,'lllllll');
                          if (!temp[val]) {
                            temp[val] = []
                          }
                          if (val !== '') {
                            temp[val].push(key)
                          }
                        })

                        Object.values(temp).forEach(val => {
                          if (val.length > 1) {
                            arr.push(...val)
                          } else {
                            arr2.push(...val)
                          }
                        })
                        const errors = {}
                        arr.forEach(key => {
                          errors[key] = '不同档位的重置金额不得相同!'
                        })
                        arr2.forEach(key => {
                          errors[key] = ''
                        })
                        setTimeout(() => {
                          field.setErrors(errors)
                        }, 0)
                      }}
                    />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值