I try to validate a name field, and for this field I like to allow the end user to add anything like Merianos Nikos
, Μέριανος Νίκος
(greek), or characters from any other language in the same form.
The form is first letter capital, rest letters of the word lower, and at least two words.
Currectly I have this regex /^([A-Z][a-z]*((s)))+[A-Z][a-z]*$/
that works perfectly with english, but not with greeks and perhaps with other languages.
Finally, I’d like to validate another field with at least on word, with the frist letter capital, but this field can also contains characters after the word.
For the moment I use the followign regex /^[sw.-_]+$/
that works, but again I have problem with greek and other languages.
Advertisement
Answer
You could do this through the use of Unicode Categories
. Thus, the regular expression ^p{Lu}p{Ll}+( p{Lu}p{Ll}+)*$
will match an upper case letter followed by one or more lower case letters, followed by 0 or more other names, seperated by spaces. An example of the expression is shown here.
With regards to your second point, you could use something of the sort ^p{Lu}p{Ll}*$
, which will expect at least 1 upper case letter followed by 0 or more lower case ones.
The expressions above assume that you do not have quotation marks, such as O'Brian
or dashes Foo-bar
in your names. If you want to handle specifically Greek names, and you know for a fact that Greek names have neither quotation marks nor dashes in them, then this should not be much of a problem.
Usually one simply ensures that the name provided is not empty, rather than specifying some strict form. Please refer to this question for more information on the matter.