简体   繁体   English

使用 React Final Form 进行自定义验证

[英]Custom Validation with React Final Form

I am trying to add some custom validations of allowing only number(amout) in the input boxes.我正在尝试在输入框中添加一些仅允许数字(金额)的自定义验证。 As per the example given on here , I tried to add custom validation.根据此处给出的示例,我尝试添加自定义验证。

const onlyAmount = value => {
  if (!value) return value
  let onlyNums = value.replace(/^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$/, '')
  return onlyNums
}

and field of my final form is like我的最终形式的领域就像

<Field
  name="price"
  component="input"
  type="text"
  parse={onlyAmount}
/>

No the problem, its not allowing me to add numbers properly.没问题,它不允许我正确添加数字。 I read the docs of the but couldn't find any helping solution.我阅读了 的文档,但找不到任何帮助解决方案。

React Final Form : https://github.com/final-form/react-final-form反应最终形式: https : //github.com/final-form/react-final-form

Any suggestions will be really helpful.任何建议都会非常有帮助。

Add just type="number".添加类型=“数字”。 The keyboard will not allow other than numbers.键盘不允许除数字以外的其他字符。

type="hidden" seems like a mistake. type="hidden"似乎是个错误。 Won't that result in a <input type="hidden"/> ?这不会导致<input type="hidden"/>吗?

I have not mentally parsed your regex, but I'd recommend you try testing your parse function in that sandbox that you linked to and see why it's not working.我没有在精神上解析您的正则表达式,但我建议您尝试在您链接到的沙箱中测试您的解析函数,看看它为什么不起作用。

It's your regex.这是你的正则表达式。 If you add a console to print out your value, then your onlyNum, you get empty string.如果你添加一个控制台来打印你的值,那么你的 onlyNum,你会得到空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM