Sorin Morovan
Sorin Morovan

Reputation: 85

Regex validate either empty or only letters

I need to validate an input to be either empty or only contain letters. The letter part I got figured out at ^[a-zA-Z]+$ . How can I accept the input if it's empty too?

Upvotes: 3

Views: 4121

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149020

Try changing your + to a *, as in this pattern:

^[a-zA-Z]*$

This will match zero or more Latin letters, so it will accept an empty string as well.

For international support, you might also want to consider:

^\p{L}*$

This will match zero or more letters in any Unicode language.

Upvotes: 10

Related Questions