Yes! Let's validate some names with RegEx.

After all, we know that all people must have a first and last name, right? And no single person has more than three or four names total? And no doubt the same person will forever be identifiable by the same name?

Plus, we know that no modern culture uses patronymic naming and people in the same nuclear family must have the same last name, right?

Well, we can at least assume that people do not have single character names, right? And there are no names that use special characters, symbols, or apostrophes?

I think your choice of RegEx to validate names is missing the point: this is a huge unwieldy problem and, even if you massively restrict the scope of names you allow, you will forever suffer the risk of false negatives and you will be turning away people from other cultures and languages. In other words, I don't think that even attempting to validate names is worth your time.

Answer from Jonathan Hersh on Stack Exchange
🌐
Medium
a-tokyo.medium.com › first-and-last-name-validation-for-forms-and-databases-d3edf29ad29d
First and Last name validation for forms and databases | by Ahmed Tokyo | Medium
May 5, 2025 - This will allow us more validation control and will make us compatible with most systems out there that use first name (given name) and last name (surname) fields separately like Banks. const NAME_REGEX = /^\w+$//** Validates a name field (first ...
🌐
CodexWorld
codexworld.com › home › how to guides › how to validate first and last name with regular expression using javascript
How to Validate First and Last Name with Regular Expression using JavaScript - CodexWorld
April 15, 2023 - In the following code snippet, ... This function is used to perform a regular expression match in JavaScript. var regName = /^[a-zA-Z]+ [a-zA-Z]+$/; var name = document.getElementById('nameInput').value; if(!regName.test(name)){ ...
🌐
RegExr
regexr.com › 3f8cm
First/Last Name Validator
Supports JavaScript & PHP/PCRE RegEx. Results update in real-time as you type. Roll over a match or expression for details. Validate patterns with suites of Tests.
🌐
GitHub
gist.github.com › a-tokyo › 80dfebf1c85ac18179de02213ed50917
First name and last name validation for forms and databases using Regex. · GitHub
Full article: https://www.noti... · 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) ...
🌐
NYC PHP Developer
andrewwoods.net › blog › 2018 › name-validation-regex
Name Validation Regex for People's Names | NYC PHP Developer | Andrew Woods
September 19, 2018 - It says that only word characters are valid. That means no spaces, no apostrophes, and no umlauts, accents, or hyphens. Word characters are limited to the English alphabet, digits, and underscores. There are millions of people with names who’ll successfully pass this regex.
🌐
Salesforce
trailhead.salesforce.com › trailblazer-community › feed › 0D54S00000E9WUwSAN
Regex Formula For First Name Validation - Trailhead
December 19, 2021 - Skip to main content · TDX registration is open! Save $600 for a limited time and join the must-attend event to experience what's next and learn how to build it
🌐
Regex Tester
regextester.com › 93601
First Name - Regex Tester/Debugger
Regular Expression to First name between 1 and 10 characters in length
Find elsewhere
🌐
PHPpot
phppot.com › javascript › validate-name-javascript
How to validate first name and last name in JavaScript? - PHPpot
February 11, 2024 - It creates a Regex pattern that only allows alphabets, spaces, or hyphens for the names. On submitting the form, the first and last names are tested to check if the pattern matches. If the validation passes, it returns the success message as “Valid name” to the UI.
🌐
regex101
regex101.com › library › gK4eN5
regex101: Name Validation
Search, filter and view user submitted regular expressions in the regex library. Over 20,000 entries, and counting!
🌐
TutorialsPoint
tutorialspoint.com › validate-the-first-name-and-last-name-with-java-regular-expressions
Validate the first name and last name with Java Regular Expressions
June 25, 2020 - In order to match the first name and last name using regular expression, we use the matches method in Java. The java.lang.String.matches() method returns a boolean value which depends on the matching of the String with the regular expression. Declaration −The java.lang.String.matches() method ...
Top answer
1 of 2
3

I have written this function for your purpose.

/**
 * This function checks if first name
 * is valid. Keep in mind this is
 * not the proper solution. It will
 * work only for names written in
 * latin letters.
 * @example
 *   isFirstNameValid('Ivan')
 *   will return true.
 *   isFirstNameValid('IvaN')
 *   will return false.
 * @author Georgi Naumov
 * [email protected] for contacts and
 * suggestions
 **/
const isFirstNameValid = (firstName) => 
  /^[a-zA-z][a-z]+$/.test(firstName)

Edit: I have implemented a solution with unicode support for the people with a similar problem in the future. It supports Latin, Hebrew and Cyrillic. If you want to support another cultures you need to provide regexes for them inside cultures hash.

const isFirstNameValidWithUnicodeSupport = (firstName, culture = 'LATIN') => {
  const cultures = {
    HEBREW: /^[\u0590-\u05FF]{2,}$/,
    CYRILLIC: /^[\u0410-\u042F\u0430-\u044F][\u0430-\u044F]+$/,
    LATIN: /^[A-Za-z][a-z]+$/,
  };
  return  cultures[culture].test(firstName);
}

This returns true because is valid name in latin
alphabet. 
console.log(isFirstNameValidWithUnicodeSupport('Ivan'));
This returns true because is valid name in cyrillic
alphabet. 
console.log(isFirstNameValidWithUnicodeSupport('Иван', 'CYRILLIC'));
This returns false because is valid name in cyrillic
alphabet but there is a space in the end. 
console.log(isFirstNameValidWithUnicodeSupport('Иван ',  'CYRILLIC'));
This returns true because is valid name in hebrew
alphabet. 
console.log(isFirstNameValidWithUnicodeSupport('אגרת',  'HEBREW'));
This returns false because is valid name in hebrew
but is whole name containing spaces. Not only first name.
console.log(isFirstNameValidWithUnicodeSupport('אגרת בת מחלת',  'HEBREW'));

Edit2: Probably better solution is xregexp library if you want to use library for that purpose. https://github.com/slevithan/xregexp

2 of 2
2

You can use regex for matching string characters.

let regex = /^[A-Za-z][a-z]+/g;

let name = "Mark";
let match = regex.exec(name);
if(match && match[0].length === name.length){
  console.log(match[0]); // Mark
}
else{
  console.log("Invalid name");
}

name = "mArk";
match = regex.exec(name);
if(match && match[0].length === name.length){
  console.log(match[0]); 
}
else{
  console.log("Invalid name");
}

To learn more about regex.

🌐
Regex Pattern
regexpattern.com › home › name validation regular expression
Name Validation Regular Expression - Regex Pattern
March 17, 2022 - A regular expression that matches and validates people's names (first name, middle name, last name).
🌐
Regexlib
regexlib.com › Search.aspx
Search Results: 22 regular expressions found.
Regular Expression Library provides a searchable database of regular expressions. Users can add, edit, rate, and test regular expressions.
🌐
Laasya Setty Blogs
laasyasettyblog.hashnode.dev › validating-username-using-regex
Validating Username Using REGEX. - Laasya Setty Blogs
November 28, 2020 - String regularExpression= "^[A-Za-z][A-Za-z0-9_]{7,29}$"; A valid username should start with an alphabet so, [A-Za-z]. All other characters can be alphabets, numbers or an underscore so, [A-Za-z0-9_]. Since length constraint was given as 8-30 ...
Top answer
1 of 6
1

Try this regex

^^- ')(?=(?![a-z]+[A-Z]))(?=(?!.*[A-Z][A-Z]))(?=(?!.*[- '][- '.]))(?=(?!.*[.][-'.]))[A-Za-z- '.]{2,}$

Demo

Edited Oct 13, 2024

My latest solution for int names:

^(?=([A-ZÀ-ÝŐŰẞŒ]|([a-zß-ÿőűœ][ '])))(?=(?![a-zß-ÿőűœ]+[A-ZÀ-ÝŐŰẞŒ]))(?=(?!.*[A-ZÀ-ÝŐŰẞŒ][A-ZÀ-ÝŐŰẞŒ]))(?=(?!.*[- '][- ']))[A-ZÀ-ÝŐŰẞŒß-ÿőűœa-z- ']{2,}([a-zß-ÿőűœ]|(, Jr.))$

function myFunction() {
    
  const pattern = "^(?=([A-ZÀ-ÝŐŰẞŒ]|([a-zß-ÿőűœ][ '])))(?=(?![a-zß-ÿőűœ]+[A-ZÀ-ÝŐŰẞŒ]))(?=(?!.*[A-ZÀ-ÝŐŰẞŒ][A-ZÀ-ÝŐŰẞŒ]))(?=(?!.*[- '][- ']))[A-ZÀ-ÝŐŰẞŒß-ÿőűœa-z- ']{2,}([a-zß-ÿőűœ]|(, Jr.))$";
    var regex = new RegExp(pattern, 'gm');
    var a = document.getElementById("myText");
  var b = a.value;
  var c = regex.test(b);
  var d = document.getElementById("result") ;
  d.innerHTML = "Result:";
  if(b != ""){
      if(c){
          d.innerHTML += " passed";
      }
      else{
        d.innerHTML += " failed";
        }
  }
  else{
    return
  }
}
input[type=text] {
  width: 99%;
  padding: 4px;
  margin: 8px 0;
  display: inline-block;
  border: 1px solid #ccc;
  box-sizing: border-box;
}
button {
  background-color: #04AA6D;
  color: white;
  padding: 4px;
  border: none;
  cursor: pointer;
  width: 25%;
}

button:hover {
  opacity: 0.8;
}
<h2>Name Validation Regex Pattern </h2>
<div class="container">
      <label for="name"><b>Name</b></label>
        <input type="text" id="myText"  placeholder="Enter Your Name" name="name" value="">
</div>
 <div class="container">

<button onclick="myFunction()">Try it</button>
<p id="result"> Result: </p>
</div>
</div>

2 of 6
0

Your expression is almost correct. The following is a modification that satisfies all of the conditions:

valid = name.matches("(?i)(^[a-z])((?![ .,'-]$)[a-z .,'-]){0,24}$");
🌐
CodexWorld
codexworld.com › home › how to guides › how to validate first and last name using regular expression in php
How to Validate First and Last Name using Regular Expression in PHP - CodexWorld
January 11, 2018 - <?php if(preg_match("/^([a-zA-Z' ]+)$/",$givenName)){ echo 'Valid name given.'; }else{ echo 'Invalid name given.'; } ... Adam Jimenez Said... ... Andrés Said... ... Do you want support for the script installation or customization? Submit your request for customization of our scripts, support for the existing web application, and new development service. You can connect with the support team directly via email at support@codexworld.com for any inquiry/help.