Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant design input validate to accept only alphabets and special characters but not numbers

Tags:

antd

I am new to ant design. I want to validate a field to take only alphabets and special characters but not numbers. This is the approach I have used so far. I am using ant design 4.

 <Form.Item
                      label="First Name"
                      name="first_name"
                      rules={[
                        { required: true, message: REQUIRED_ERROR_MESSAGE },
                       {
                        pattern: new RegExp("/^[A-Z@~`!@#$%^&*()_=+\\\\';:\"\\/?>.<,-]*$/i"),
                        message: "field does not accept numbers"
                       }
                      ]}
                    >
                      <Input  />
  </Form.Item>

The above approach fails even if a user enters only letters. any help would be appreciated.

like image 441
Clement Okyere Avatar asked Dec 18 '25 19:12

Clement Okyere


1 Answers

try removing the quotes that you currently have around your regular expression like this:

pattern: new RegExp(/^[a-zA-Z@~`!@#$%^&*()_=+\\\\';:\"\\/?>.<,-]+$/i),

here's an example on codesandbox

like image 116
jcklopp Avatar answered Dec 20 '25 13:12

jcklopp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!