A Regular Expression to filter invalid names

Kumar Abhishek's avatar Kumar Abhishek 1 min read 🔤

A regular expression that can be used as a basic filter to detect invalid names.

  • I use it in an Indian context. May not be useful for others.
  • Use it with an ‘i’ RegEx flag to ignore case.
  • Filters out spelling mistakes & gibberish entries; but not all invalid entries!
  • Similarly useful for other English nouns as well, and not just names.
  • Useful for quick frontend validation in scenarios where users may tap gibberish to quickly skip a mandatory ‘name’ parameter.
^(?!(?:(?:([a-z]) *\1(?: *\1)*)|(?:.*?(?:(?:(?:^|[^d])([a-z])\2\2)|(?:d([a-df-z])\3\3)).*)|(?:.*?([a-z]{3,})\4\4).*|(?:.*(?:^|[^a-z])[^aeiou \.]{4,}(?:$|[^a-z]).*))$)(?:[a-z]+\.? ){0,2}[a-z]+$

Logic §

  1. Allows up to three words (first & last name).
  2. Allows abbreviations (ending in ‘.’) in all but the last word.
  3. Rejects repetition of any subset of characters.
  4. Uses rules of pronounceable English syllables to filter bad entries (based on my limited research):
    1. A consonant cannot be consecutively repeated.
    2. A vowel cannot be consecutively repeated more than twice.
      1. Exception: e that can be repeated thrice for certain Indian names, for example, those ending with ‘deee’.
  5. (more to be updated soon)

Note:
I had written this long back by analyzing invalid entries made in a fin-tech app used by agents to make entries on behalf of other customers. I will update the exact logic and describe parts of the regex soon!

Test it here & get a sample code in Javascript:

All Notes Suggest Edit