4

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 /^[\s\w\.\-_]+$/ that works, but again I have problem with greek and other languages.

KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166

3 Answers3

5

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.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
  • thanks for your reply. This looks to be the best solution until now. The problem I have now, is that the given regex doesn't work with JS. The regex101.com, when I use the JS understands the `\p` as literal. Do you have any idea ? – KodeFor.Me Jul 21 '15 at 09:01
  • @MerianosNikos: [This](http://stackoverflow.com/questions/280712/javascript-unicode-regexes) might be of help. That being said, I would not recommend you use a regular expression to check for *valid* names. – npinti Jul 21 '15 at 09:05
  • This fails to allow Chinese and Japanese character, since they belong to `Lo`. There are some Greek characters in `Lt`, also. – nhahtdh Jul 21 '15 at 09:05
  • @nhahtdh: In that case, I presume that using an `or` like operator with `\p{Lo}+` or `[\p{Lo} ]+` should work. – npinti Jul 21 '15 at 09:09
3
^[{\p{L}}{0-9}]$

This regex matches any kind of letter from any language (and also numbers).

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
JaRiGoF
  • 31
  • 2
2
function isFullname($fullname) {
   return preg_match("/^((?:\p{Ll}|\p{Lu}){2,30}\s?){2,4}$/g", $fullname);
}

This is useful for me. Because the username may also be written in lowercase letters.

And it can have a name or surname of at least 2 characters. Also, I accept a name with a maximum of 30 characters. And I make it repeatable at least 2 times at most 4 times.

It could have a name like McCésy (realy? =)) ...

tutkun
  • 35
  • 8