2

My current regex is /^[a-zA-Z]+( [a-zA-Z]+)*$/. It works for names like:

John Smith

Anyway, the site is international and name could be like this, for example:

Jānis Bērziņš

It's a valid name, but regex will fail to validate it because of ā, ē and š chars.

The one way would be to type all chars that are allowed, but then the list would be HUGE!

I'm looking for easier way to do that. Maybe black-list instead of white-list approach?

Thanks in any advice!

daGrevis
  • 21,014
  • 37
  • 100
  • 139

2 Answers2

6

Use a regex in UTF-8 mode with the appropriate Unicode character propert(y/ies).

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

Just replace your character class with the unicode property for letter \pL

/^\pL+( \pL+)*$/

See here on regular/expressions.info an overview of the unicode properties

in my test on writecodeonline.com this is working

$s = "Jānis Bērziņš";
preg_match("/^\\pL+( \\pL+)*$/", $s, $matches);
stema
  • 90,351
  • 20
  • 107
  • 135