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
1 Answer
Reset to default 8You 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
.