Fullname multilingual Regexp

Viewed 53

Currently the validation of fullname looks like:

/^[a-zA-Z ]{2,30}$/

But that regexp validates only latin alphabet names. This should be changed in order to handle multilingual characters also. I have tried:

/^(\p{L}\p{M}*){2,30}$/u

But it validates numbers within names also, which is not correct.

1 Answers

As in the first case, use a character class with Unicode as well:

/^[\p{L}\p{M}\p{Zs}]{2,30}$/u

The \p{Zs} denotes a space char, such as regular space and Japanese space char 怀.

In case you want to prevent space at the start and end, use these negative lookaheads:

/^(?!\p{Zs})(?!.*\p{Zs}$)[\p{L}\p{M}\p{Zs}]{2,30}$/u

See a demo on regex101.com.

Related