javascript - 当用户输入超过 `one` 或 1 个字符时如何显示错误?

标签 javascript reactjs antd

我正在为我的应用程序使用ant designhttps://ant.design/components/table/ 我面临一个验证问题。我在演示中实现了表格。当我单击编辑按钮时,它会显示输入字段

我能够添加对必需的验证。因此不允许为空。但我希望用户在输入中仅输入一个或1个字符 code> 字段,否则显示错误。

这是我的代码 https://codesandbox.io/s/thirsty-tesla-1nwph

return (
      <td {...restProps}>
        {editing ? (
          <Form.Item style={{ margin: 0 }}>
            {getFieldDecorator(dataIndex, {
              rules: [
                {
                  required: true,
                  message: `Please Input ${title}!`
                }
              ],
              initialValue: record[dataIndex]
            })(getInput())}
          </Form.Item>
        ) : (
          children
        )}
      </td>
    );
  };

表单错误 https://ant.design/components/form/

enter image description here

最佳答案

I am able to add validation of required .so empty is not allowed .but I want user only enter one or 1 character inside the input field else it shows an error.

如果您说“只有文字 1one 在输入中才有效”,您可以使用

              rules: [
                {
                  required: true,
                  message: `Please Input ${title}!`
                },
                {
                  pattern: /^(1|one)$/,
                  message: "Only 1 or one are valid"
                }
              ],

否则,如果它是长度,您可以使用 max 属性。所有验证规则的列表可以在此处查看:https://ant.design/components/form/?locale=en-US#Validation-Rules

关于javascript - 当用户输入超过 `one` 或 1 个字符时如何显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58121527/

相关文章:

javascript - 当数据库更改时为每个人更新 DOM - MongoDB、Ajax、Express、React

javascript - 如何调用使用状态(由另一个函数设置)并更新状态的函数

css - 如何在 React UI 语义中设置样式、对齐内容

javascript - Antd Modal如何更新其中的值

javascript - 如何在ant design中自定义和更改菜单的颜色?

javascript - toLocaleString() 在 Safari 浏览器中不起作用

javascript - Kendo Grid 无法识别 Grid 事件生成器函数 "FilterMenuInit"

javascript - Promise 在解决之前被过早拒绝多次

reactjs - Antd - 有没有办法改变卡片标题的背景颜色?

javascript - 如何在十六进制网格上用图像替换悬停文本