-1

How to use regex to check multilingual names.

Letters like: üöäõ

My code:

$.validator.addMethod("lettersonly_firstName",function function_name(value,element) {
    return this.optional(element) || /^[a-zA-Z]+$/g.test(value);
}, "Plese enter a valid first Name.");
nkc87002
  • 13
  • 1
  • 1
    Check out https://stackoverflow.com/questions/20690499/concrete-javascript-regex-for-accented-characters-diacritics. Not only does it answer your question, it also is a great example of how to ask a question!! – junvar Aug 22 '19 at 21:26
  • Why use a regex at all for a first name? What are multilingual names? Chinese characters? [It's fairly easy to be too restrictive](https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/). Just simply accept what the user enters. – Ivar Aug 22 '19 at 21:28
  • 1
    Possible duplicate of [Concrete Javascript Regex for Accented Characters (Diacritics)](https://stackoverflow.com/questions/20690499/concrete-javascript-regex-for-accented-characters-diacritics) – manveti Aug 22 '19 at 23:32

1 Answers1

0

You can choose particular Unicode blocks explicitly by providing a range of characters:

[-a-zA-ZÀ-ÿĀ-ſƀ-ɏ ']+

It covers characters , -, '. Which could be a part of the name in different cultures.

Plus these Unicode blocks:

If it's not required, you can remove the last one. Or you can also add Latin Extended-C, Latin Extended-D, and Latin Extended-E.

Latin script in Unicode

If you need, you can add Cyrillic also:


Don't forget to test your Regex. Even better is to write automated tests against some name list.

You can check out the regex and play with it there: https://regex101.com/r/ZfEE98/2

Vlad DX
  • 4,200
  • 19
  • 28