Reputation: 85
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
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