我正在为我的应用程序使用ant design
。
https://ant.design/components/table/
我面临一个验证问题。我在演示中实现了表格。当我单击编辑
按钮时,它会显示输入字段
我能够添加对必需
的验证。因此不允许为空。但我希望用户在输入中仅输入
code> 字段,否则显示错误。一个
或1个字符
这是我的代码 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>
);
};
最佳答案
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.
如果您说“只有文字 1
或 one
在输入中才有效”,您可以使用
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/