【问题标题】:Antd validation by pattern do not trigger errorAntd通过模式验证不会触发错误
【发布时间】:2020-02-07 02:15:02
【问题描述】:

所以我读到了这个: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" " - 它需要被删除而不是添加。您可以在那里查看附加的代码沙箱:)
  • @blueseal 啊...确实 xD 谢谢

标签: javascript regex antd


【解决方案1】:

您不必明确提及type: "regexp"

做这样的事情,它会工作。

rules: [
 { required: true, message: "Please input your username!" },
 {
    pattern:/^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[!#$%\-_=+<>])([a-zA-Z0-9!#$%\-_=+<>]+)$/,
    message: `Password Pattern`
 }
]

查看与regex 相关的类似answer

【讨论】:

    猜你喜欢
    • 2015-07-24
    • 2019-04-08
    • 2013-10-14
    • 2018-06-09
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 2014-09-30
    相关资源
    最近更新 更多