所以我读了这个:Regular expression works on regex101.com, but not on prod
我在 antd 中创建了以下规则:Demo
<Form.Item
validateStatus={usernameError ? "error" : ""}
help={usernameError || ""}
>
{getFieldDecorator("username", {
rules: [
{ required: true, message: "Please input your username!" },
{
type: "regexp",
pattern: new RegExp(
/^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!#$%\-_=+<>])([a-zA-Z0-9!#$%\-_=+<>]+)$/
),
message: `Password Pattern`
}
]
})(
<Input
prefix={<Icon type="user" style={{ color: "rgba(0,0,0,.25)" }} />}
placeholder="Username"
/>
)}
</Form.Item>
正则表达式应匹配任何必须包含至少 1 个数字、1 个字母和 1 个特殊字符的内容。
从日志中可以看出,正则表达式在 JS 中可以正常工作,但在 antd 中,该模式不起作用。
另外,我关注了this我正确地添加了 type="regexp"
还缺少什么?
最佳答案
您不必明确提及 type: "regexp"
。
像这样做,它会起作用。
rules: [
{ required: true, message: "Please input your username!" },
{
pattern:/^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!#$%\-_=+<>])([a-zA-Z0-9!#$%\-_=+<>]+)$/,
message: `Password Pattern`
}
]
查看类似的 answer与 regex
相关。
关于javascript - Antd 通过模式验证不会触发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60106226/