Someone
Someone

Reputation: 10575

Regex to enter only alphabets and donot enter any other characters

I have a Regex which contains the following .Is this suitable restricting the user only to enter alphabets and do not allow numbers ,special characters such as (',@#$%^&*())

^[a-zA-Z]+$

Upvotes: 0

Views: 17164

Answers (3)

Helge Klein
Helge Klein

Reputation: 9095

That regex will only match alphanumeric characters from a to z, upper- and lowercase. So I guess that is what you wanted. By the way: because of the plus sign, at least one of the characters from your character class is required.

Upvotes: 1

Tim Pietzcker
Tim Pietzcker

Reputation: 336498

Yes, but it also does not allow accented characters and other non-ASCII letters (ä, à, ñ, ß and many others).

^\p{L}+$ is an alternative for this scenario, if your regex engine is Unicode-aware.

Upvotes: 6

wimh
wimh

Reputation: 15232

^[a-zA-Z]+$

Yes, it only allows a..z and A..Z. This means a space is also not allowed. At least one character, so an empty string is not matched either.

Upvotes: 2

Related Questions