最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Antd validation by pattern do not trigger error - Stack Overflow

programmeradmin1浏览0评论

So I read this : Regular expression works on regex101, but not on prod

I create the following rule in 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>

The regex should match anything that MUST include at least 1 number, 1 letter and 1 special character.

As you can see from the log, the regex work correctly in JS, but in antd, the pattern is not working.

Also, I followed this and I correctly added the type="regexp"

What is still missing ?

So I read this : Regular expression works on regex101., but not on prod

I create the following rule in 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>

The regex should match anything that MUST include at least 1 number, 1 letter and 1 special character.

As you can see from the log, the regex work correctly in JS, but in antd, the pattern is not working.

Also, I followed this and I correctly added the type="regexp"

What is still missing ?

Share Improve this question asked Feb 7, 2020 at 2:15 CrocsxCrocsx 7,64015 gold badges86 silver badges177 bronze badges 2
  • "Also, I followed this and I correctly added the type="regexp" " - it needs to be removed not added. You can check out the attached code sandbox there :) – blueseal Commented Feb 9, 2020 at 11:23
  • @blueseal ah... indeed xD thanks – Crocsx Commented Feb 10, 2020 at 1:25
Add a ment  | 

1 Answer 1

Reset to default 8

You don't have to explicitly mention type: "regexp".

Do something like this, It will work.

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

Check out a similar answer related to regex.

发布评论

评论列表(0)

  1. 暂无评论