简体   繁体   English

如何防止在 Form.Item ant 的 InputNumber 中输入超出范围的非数字符号和数字

[英]How to prevent entering non numeric symbols and numbers out of range in InputNumber in Form.Item ant

I tried to handle it in onChange through controlled input.我试图通过受控输入在onChange中处理它。

Tried to handle it in formatter and in parser but invalid values doesn't trigger it, despite the fact that symbols entering in input.尝试在formatterparser中处理它,但无效值不会触发它,尽管符号输入输入。

Tried to set value in this input by setFieldsValue method that provided by useForm hook.尝试通过useForm钩子提供的setFieldsValue方法在此输入中设置值。

Tried to set type="number"试图设置type="number"

Nothing of this works while InputNumber is inside Form.Item .InputNumberForm.Item内时,这些都不起作用。 Link to Sandbox 沙盒链接

Please help.请帮忙。 How I can resolve it?我该如何解决?

Check the following example.检查以下示例。

Following code will allow user to enter only the numbers以下代码将允许用户仅输入数字

App.js应用程序.js

import React from 'react';
import 'antd/dist/antd.css';
import './index.css';
import { InputNumber, Form } from 'antd';

const App = () => {
  return (
    <Form>
      <Form.Item name="input-with-type">
        <InputNumber
          type="number"
          min={0}
          max={9999.99}
          onKeyDown={(event) => {
            const re = /^[0-9\b]+$/;
            if (!re.test(event.key) && event.key !== 'Backspace') {
              event.preventDefault();
            }
          }}
        />
      </Form.Item>
    </Form>
  );
};

export default App;

Note : you can change the regex expression according to your requirements注意:您可以根据您的要求更改正则表达式

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

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