Skip to content

Instantly share code, notes, and snippets.

@a-tokyo
Last active May 28, 2021 15:17
Show Gist options
  • Save a-tokyo/80dfebf1c85ac18179de02213ed50917 to your computer and use it in GitHub Desktop.
Save a-tokyo/80dfebf1c85ac18179de02213ed50917 to your computer and use it in GitHub Desktop.
First name and last name validation for forms and databases using Regex.

Full article: https://www.notion.so/ahmedtokyo/First-and-Last-name-validation-for-forms-and-databases-ab29ca4a5d7a4f848375904649b3be6e

International first and last name validation

This will work pretty well with all world regions.

const NAME_REGEX = /^[a-zA-Z\xC0-\uFFFF]+([ \-']{0,1}[a-zA-Z\xC0-\uFFFF]+){0,2}[.]{0,1}$/

/** Validates a name field (first or last name) */
const isValidName = (name) => NAME_REGEX.test(name)

Western support

This will work pretty well with english-like names covering the regions of North America, South America, Western Europe and Australia.

const NAME_REGEX = /^[a-zA-Z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u01FF]+([ \-']{0,1}[a-zA-Z\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u01FF]+){0,2}[.]{0,1}$/

Arabic only support

This will work pretty well with the MENA Region (North Africa and the middleast, except certian countries that have multiple minor languages.).

const NAME_REGEX = /^[\u0600-\u06FF\u066E-\u06D5\u0750−\u077F]+([ ]{0,1}[\u0600-\u06FF\u066E-\u06D5\u0750−\u077F]+){0,2}$/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment