Vivek Shankar
Vivek Shankar

Reputation: 701

Allow only alphabets and special characters

I want an input field to allow only alphabets and special characters. Can someone specify it

/[ A-Za-z@]/i

Upvotes: 0

Views: 17439

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627607

You may use

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

See regex demo.

Due to /i modifier, you do not have to specify a-z range in the character class as case-insensitivity is enabled.

The regex matches:

  • ^ - start of string
  • [A-Z@~`!@#$%^&*()_=+\\\\';:\"\\/?>.<,-]* - 0 or more characters inside the A-Za-z range and all the special ASCII characters (you can add more if I missed any or if you need to support more)
  • $ - end of string.

Upvotes: 4

Related Questions