199

For website validation purposes, I need first name and last name validation.

For the first name, it should only contain letters, can be several words with spaces, and has a minimum of three characters, but a maximum at top 30 characters. An empty string shouldn't be validated (e.g. Jason, jason, jason smith, jason smith, JASON, Jason smith, jason Smith, and jason SMITH).

For the last name, it should be a single word, only letters, with at least three characters, but at most 30 characters. Empty strings shouldn't be validated (e.g. lazslo, Lazslo, and LAZSLO).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hellnar
  • 62,315
  • 79
  • 204
  • 279
  • 18
    What about first names like 'Jo'? – a'r Mar 05 '10 at 09:44
  • 12
    just a remark: hyphens are common in lastnames ... maybe there are lastnames with spaces, too – tanascius Mar 05 '10 at 09:45
  • This validation will be for a non english site thus in my country less or more smallest name is 3 letters :) Also it will be used for a facebook connect to my e commerce site entegration. Thus some stupid facebook names like "Danielle Cage ッ" or "Hopee Lamb-" shouldnt be validated, user will be asked to rewrite their names - sirnames in that case :) – Hellnar Mar 05 '10 at 09:50
  • 2
    Note: a regularexpressionvalidator will ignore empty inputs: this might or might not be what you want. – Hans Kesting Mar 05 '10 at 09:59
  • 5
    If at all possible, unless you have an amazingly compelling reason for requiring a first and last name, just provide a single "Name" field. http://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ – Chris Sep 12 '11 at 12:50
  • Khoisan names, amongst others, makes use of special characters like "!". Irish surnames may make use of apostrophes. – CShark Mar 11 '16 at 10:29
  • Although I'm against any strict validation of names, in some scenarios it is vital to validate. For example for ID verification, the name user types into the form must match exactly to the names in the documents (e.g. drivers licence, passport, etc.). And you might be surprised how often people make typos or put special characters into the name fields. Make sure you provide a good error messaging as well. – Alexander Burakevych Sep 08 '17 at 01:19
  • According to ICAO which also defines standards for international passports, the max number of characters in the name must be 31. – Alexander Burakevych Sep 08 '17 at 01:24
  • **Why?** A user's first and last name consists of whatever they decide to use. This is a legal fact. You don't have any *requirement* to 'validate' it at all. – user207421 May 30 '18 at 09:52
  • 10
    or what about "X Æ A-12" https://twitter.com/elonmusk/status/1257508900812713984 – Rishi Dua May 07 '20 at 20:02
  • @AlexanderBurakevych I am having a hard time finding a document defining the passport standard. Could you link it here please ? – Tchypp Jan 04 '21 at 08:39
  • Having multi language support is essential, BUT would it be acceptable to limit any given word to only contain one alphabet to avoid mixing look alike characters to spoof other registered names? – KalleMP Nov 21 '22 at 08:56

18 Answers18

299

Don't forget about names like:

  • Mathias d'Arras
  • Martin Luther King, Jr.
  • Hector Sausage-Hausen

This should do the trick for most things:

/^[a-z ,.'-]+$/i

OR Support international names with super sweet unicode:

/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.'-]+$/u

John Smith
  • 496
  • 1
  • 6
  • 20
maček
  • 76,434
  • 37
  • 167
  • 198
  • I can't save this with č charactor my editor is showing exceptions . . what wil do? – coderex Aug 29 '11 at 17:50
  • @coderex, utf8-encoded characters in your source code are not supported by all programming languages. – maček Oct 03 '11 at 15:30
  • 2
    macek, you're missing quite a few characters to support even common Latin-using languages (Polish, for example). This further demonstrates that the whole effort is simply futile. – Joey Dec 30 '11 at 18:31
  • 5
    I would escape the [special characters](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp#Special_characters_in_regular_expressions) in these regexps - especially `.` (decimal point/dot/full stop) since it's the regexp wildcard =) – Joel Purra Aug 08 '12 at 18:45
  • @murmux: it seems you are right! All theses years escaping dots for naught! I believe I developed the habit when coding in [classic JScript](http://en.wikipedia.org/wiki/JScript) - not sure if it made a difference back then either =) – Joel Purra Sep 22 '12 at 13:51
  • 40
    You cannot validate all the possible national characters. For example Hungarian characters `őŐűŰ` are missing, Polish characters `łŁ` as well, not to mention a number of Lithuanian and Latvian characters. Rather try to find a library which transforms the exotic characters into the proper accent-less version, then write the `/^[a-z ,.'-]+$/i` regexp. – gaborsch Mar 02 '13 at 16:53
  • 105
    So is `陳大文` not a valid name here? – Alvin Wong Apr 11 '13 at 00:53
  • 25
    For Unicode, use `/^[\p{L}'][ \p{L}'-]*[\p{L}]$/u`, however pretty forgiving. – Frederik Krautwald Nov 18 '14 at 14:45
  • And what about missing czech characters "řďťňŘĎŤŇ"? ;) – Máťa - Stitod.cz Apr 30 '15 at 08:10
  • 1
    You should've included A-Z, like so: ^[A-Za-z ,.'-]+$ – Mark May 14 '15 at 17:22
  • 7
    A-Z does not need to be included in the original example because the i modifier after the expression means ignore case. – mhanney Jul 21 '15 at 16:18
  • This will include 'names' like ... or foo.'-asd. I think ^([a-z]+[,.]?[ ]?|[a-z]+['-]?)+$ this is will remove those cases. – Asim K T Sep 11 '15 at 06:49
  • This allows dot and spaces to be submitted and names – Prarthana Hegde Mar 04 '16 at 09:44
  • 3
    I use this, `^[\p{L} ,.'-]*$` which is working for Unicode too – sky91 May 28 '16 at 04:40
  • `SyntaxError: Invalid flags supplied to RegExp constructor 'u'` – Green Oct 11 '16 at 22:42
  • There are also multiple ways to get the basically same character that you would have to add in the special characters string, i.e.: \xc1A vs \u0301 – Radio Controlled Jan 12 '17 at 14:14
  • I can't put this to work. It's accepting only the first name, as valid. – Luís Assunção May 07 '17 at 08:13
  • you does not handle white spaces – Abhishek Garg Mar 15 '19 at 08:23
  • 1
    I do not find it correct because it accepts only spaces as a valid name – Rez.Net Oct 14 '20 at 22:03
  • you should add "æ" to that; – Pantani Feb 03 '21 at 10:20
  • 3
    So is `Иванов Иван Иванович` not a valid name here? You make false assumptions on the format of first and last name. It is probably better not to validate the name at all, apart from checking that it is empty. – Andrei Krasutski Dec 16 '21 at 21:30
  • If I enter multiple space only it accept it – Reza Feb 25 '22 at 17:36
  • will it work for non normalized characters? – andigor Oct 12 '22 at 08:42
136

You make false assumptions on the format of first and last name. It is probably better not to validate the name at all, apart from checking that it is empty.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • 7
    This is the only sane answer. See also https://www.kalzumeus.com/2010/06/17/falsehoods-programmers-believe-about-names/ . – Emil Jeřábek Sep 17 '20 at 08:32
  • 2
    Be careful of JavaScript or HTML injection attacks if you're not validating. – rudivonstaden Nov 18 '20 at 07:42
  • But you probably don't want to have a name with Emoji's (or character-based emoticons) – Matthijs Feb 09 '21 at 08:45
  • 2
    Can't have numbers or symbols in a name though, can we? We want a "Name" not a "@username" after alll. – pragmateek Apr 11 '21 at 16:26
  • 1
    @pragmateek Yes, it can. Apparently, states like Hawaii accept all symbols on a standard US keyboard, and others like Washington accept numbers. As Sjoerd said, it's better not to make false assumptions. – Ricardo Yubal Jan 05 '22 at 05:16
  • yeah hey, let's not be discriminatory against baby elon X Æ A-12!!! – fullStackChris Jan 24 '22 at 16:58
  • The answer is only "sane" if you don't need to interact with any downstream systems (which you can't change) that are making false assumptions about names. – Joshua Clark Dec 05 '22 at 05:06
  • This is indeed a terrible question, so I'm upvoting this answer even though it's more of a comment. – miken32 May 17 '23 at 20:20
64

After going through all of these answers I found a way to build a tiny regex that supports most languages and only allows for word characters. It even supports some special characters like hyphens, spaces and apostrophes. I've tested in python and it supports the characters below:

^[\w'\-,.][^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]]{2,}$

Characters supported:

abcdefghijklmnopqrstwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
áéíóúäëïöüÄ'
陳大文
łŁőŐűŰZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųū
ÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁ
ŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ.-
ñÑâê都道府県Федерации
আবাসযোগ্য জমির걸쳐 있는
CristianGuerrero
  • 1,263
  • 9
  • 16
  • 3
    Out of all the answers this one worked like a charm using ng 4. – Deniss M. Sep 10 '17 at 11:32
  • 6
    Your regex fail on 2 characters string. I think this fixed it `^[\w'\-,.]*[^_!¡?÷?¿\/\\+=@#$%ˆ&*(){}|~<>;:[\]]*$` – TKA Aug 08 '19 at 19:23
  • 2
    National characters are supported but not on the first position. I'd change expression to `^[^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]]{2,}$` – TOUDIdel Oct 24 '19 at 19:12
  • 2
    We have the common lastname Österreicher in Austria and it's invalid. Also according to regex101.com the forward slash needs to be escaped. I cannot recommend this... – CodingYourLife Oct 11 '20 at 15:04
  • 2
    So Emoji's are valid ? – Matthijs Feb 09 '21 at 08:47
  • 1
    You need to escape the `/` so that would be `^[\w'\-,.][^0-9_!¡?÷?¿\/\\+=@#$%ˆ&*(){}|~<>;:[\]]{2,}$` – Marc Sep 05 '22 at 06:41
  • @CodingYourLife "Österreicher" is matched by this RegEx. – Marc Sep 05 '22 at 06:43
  • This RegEx matches newlines which is incorrect! It also doesn't match `陳大文 `. I suggest `^[^\n0-9_!¡?÷?¿\/\\+=@#$%ˆ&*(){}|~<>;:[\]]{2,}$` – Marc Sep 05 '22 at 06:47
  • 1
    This will match even when national character is in first position @TOUDIdel : `/^[\p{L}'\-,.][^0-9_!¡?÷?¿/\\+=@#$%ˆ&*(){}|~<>;:[\]]{2,}$/u` I have edited the original answer with this as it seems like a bug in the original regex. – Capaj Jan 07 '23 at 11:57
  • At the least, html tags should be stripped. – David Latty Jun 05 '23 at 21:26
47

I have created a custom regex to deal with names:

I have tried these types of names and found working perfect

  1. John Smith
  2. John D'Largy
  3. John Doe-Smith
  4. John Doe Smith
  5. Hector Sausage-Hausen
  6. Mathias d'Arras
  7. Martin Luther King
  8. Ai Wong
  9. Chao Chang
  10. Alzbeta Bara

My RegEx looks like this:

^([a-zA-Z]{2,}\s[a-zA-Z]{1,}'?-?[a-zA-Z]{2,}\s?([a-zA-Z]{1,})?)

MVC4 Model:

[RegularExpression("^([a-zA-Z]{2,}\\s[a-zA-Z]{1,}'?-?[a-zA-Z]{2,}\\s?([a-zA-Z]{1,})?)", ErrorMessage = "Valid Charactors include (A-Z) (a-z) (' space -)") ]

Please note the double \\ for escape characters

For those of you that are new to RegEx I thought I'd include a explanation.

^               // start of line
[a-zA-Z]{2,}    // will except a name with at least two characters
\s              // will look for white space between name and surname
[a-zA-Z]{1,}    // needs at least 1 Character
\'?-?           // possibility of **'** or **-** for double barreled and hyphenated surnames
[a-zA-Z]{2,}    // will except a name with at least two characters
\s?             // possibility of another whitespace
([a-zA-Z]{1,})? // possibility of a second surname
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Francois Muller
  • 558
  • 4
  • 8
  • 1
    I have a unit test that tests several names, then several things that are not names. The not names list has 'test token' as the first entry. This matches that. – Rob Mar 23 '17 at 22:01
  • It's almost what i was looking for. I guess (maybe wrongly) that you are a french guy. And one case is not handle that you can encounter in French : composed names, like Jean-Pierre, Marie-Charlotte, etc...I'm new to reg exp but i guess a -? like you did for last name, in between the 2 first words, could do the job. – Linpter Feb 28 '18 at 10:20
  • Hi Linpter, Not French, however I do have a French name. Yes. I have not tested this, however you should be able to add the - in the first [ ] so change: ^([a-zA-Z]{2,}\s[a-zA-z]{1,}'?-?[a-zA-Z]{2,}\s?([a-zA-Z]{1,})?) TO: ^([a-zA-Z -]{2,}\s[a-zA-z]{1,}'?-?[a-zA-Z]{2,}\s?([a-zA-Z]{1,})?) – Francois Muller Mar 03 '18 at 11:19
  • Does not work for abbreviations like. John F. Kennedy – DAG Jun 09 '21 at 10:06
  • you should be able to add "." in the line \'?-? and add period that might catch this. example '?-?.? note that this is not tested and going on 3 year old knowledge – Francois Muller Jun 18 '21 at 08:08
  • This doesn't work with names such as Fabián, Julián, Ramírez, López. All pretty common names and last names in Latin America. – Ricardo Yubal Jan 05 '22 at 05:19
17

I've tried almost everything on this page, then I decided to modify the most voted answer which ended up working best. Simply matches all languages and includes .,-' characters.

Here it is:

/^[\p{L} ,.'-]+$/u
John Boga
  • 484
  • 6
  • 14
  • 2
    Probably the most sensed answer here. This Regex will block only numbers but will accomodate various name formats. However if you care about Internet Explorer 11 this one uses the unicode Regex prototype, which is not available in old browsers – MacK Feb 21 '22 at 12:10
  • 1
    You might improve this by disallowing single-letter names `^[\p{L} ,.'-]{2.}$`. So `Jo" is OK but not "J". – Marc Sep 05 '22 at 06:59
  • Your regex matches the following strings: `.-..,.,..,.,-,.,.,.,-----.,.,.,.,.,.,---.,.,.,`, `GSDDSGSDHSDBKSDGFDPKNDRJFBDFDbdfbdfkbjdfDFGDFG`, `'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''`, `.'.'.'-'.'.'.'bdf.'-.'.'-'.'.'.sdgscdbsf'.''..''.---`, `sdgk ldksjfgklsdjgklsdjgklsdjklBCBCBCBbjkds`, `'-shS'-xfbX,,,,,xdg'''xx'cxb,--'',,---xfvb,.ZgzdzxSA safgasf`. To put it bluntly, your regex is terrible. It matches almost all full-length sentences. – Thegerdfather Oct 20 '22 at 04:18
16

I have searched and searched and played and played with it and although it is not perfect it may help others making the attempt to validate first and last names that have been provided as one variable.

In my case, that variable is $name.

I used the following code for my PHP:

    if (preg_match('/\b([A-Z]{1}[a-z]{1,30}[- ]{0,1}|[A-Z]{1}[- \']{1}[A-Z]{0,1}  
    [a-z]{1,30}[- ]{0,1}|[a-z]{1,2}[ -\']{1}[A-Z]{1}[a-z]{1,30}){2,5}/', $name)  
    # there is no space line break between in the above "if statement", any that   
    # you notice or perceive are only there for formatting purposes.  
    # 
    # pass - successful match - do something
    } else {
    # fail - unsuccessful match - do something

I am learning RegEx myself but I do have the explanation for the code as provided by RegEx buddy.
Here it is:

Assert position at a word boundary «\b»

Match the regular expression below and capture its match into backreference number 1
«([A-Z]{1}[a-z]{1,30}[- ]{0,1}|[A-Z]{1}[- \']{1}[A-Z]{0,1}[a-z]{1,30}[- ]{0,1}|[a-z]{1,2}[ -\']{1}[A-Z]{1}[a-z]{1,30}){2,5}»

Between 2 and 5 times, as many times as possible, giving back as needed (greedy) «{2,5}»

* I NEED SOME HELP HERE WITH UNDERSTANDING THE RAMIFICATIONS OF THIS NOTE *

Note: I repeated the capturing group itself. The group will capture only the last iteration. Put a capturing group around the repeated group to capture all iterations. «{2,5}»

Match either the regular expression below (attempting the next alternative only if this one fails) «[A-Z]{1}[a-z]{1,30}[- ]{0,1}»

Match a single character in the range between “A” and “Z” «[A-Z]{1}»

Exactly 1 times «{1}»

Match a single character in the range between “a” and “z” «[a-z]{1,30}»

Between one and 30 times, as many times as possible, giving back as needed (greedy) «{1,30}»

Match a single character present in the list “- ” «[- ]{0,1}»

Between zero and one times, as many times as possible, giving back as needed (greedy) «{0,1}»

Or match regular expression number 2 below (attempting the next alternative only if this one fails) «[A-Z]{1}[- \']{1}[A-Z]{0,1}[a-z]{1,30}[- ]{0,1}»

Match a single character in the range between “A” and “Z” «[A-Z]{1}»

Exactly 1 times «{1}»

Match a single character present in the list below «[- \']{1}»

Exactly 1 times «{1}»

One of the characters “- ” «- » A ' character «\'»

Match a single character in the range between “A” and “Z” «[A-Z]{0,1}»

Between zero and one times, as many times as possible, giving back as needed (greedy) «{0,1}»

Match a single character in the range between “a” and “z” «[a-z]{1,30}»

Between one and 30 times, as many times as possible, giving back as needed (greedy) «{1,30}»

Match a single character present in the list “- ” «[- ]{0,1}»

Between zero and one times, as many times as possible, giving back as needed (greedy) «{0,1}»

Or match regular expression number 3 below (the entire group fails if this one fails to match) «[a-z]{1,2}[ -\']{1}[A-Z]{1}[a-z]{1,30}»

Match a single character in the range between “a” and “z” «[a-z]{1,2}»

Between one and 2 times, as many times as possible, giving back as needed (greedy) «{1,2}»

Match a single character in the range between “ ” and “'” «[ -\']{1}»

Exactly 1 times «{1}»

Match a single character in the range between “A” and “Z” «[A-Z]{1}»

Exactly 1 times «{1}»

Match a single character in the range between “a” and “z” «[a-z]{1,30}»

Between one and 30 times, as many times as possible, giving back as needed (greedy) «{1,30}»

I know this validation totally assumes that every person filling out the form has a western name and that may eliminates the vast majority of folks in the world. However, I feel like this is a step in the proper direction. Perhaps this regular expression is too basic for the gurus to address simplistically or maybe there is some other reason that I was unable to find the above code in my searches. I spent way too long trying to figure this bit out, you will probably notice just how foggy my mind is on all this if you look at my test names below.

I tested the code on the following names and the results are in parentheses to the right of each name.

  1. STEVE SMITH (fail)
  2. Stev3 Smith (fail)
  3. STeve Smith (fail)
  4. Steve SMith (fail)
  5. Steve Sm1th (passed on the Steve Sm)
  6. d'Are to Beaware (passed on the Are to Beaware)
  7. Jo Blow (passed)
  8. Hyoung Kyoung Wu (passed)
  9. Mike O'Neal (passed)
  10. Steve Johnson-Smith (passed)
  11. Jozef-Schmozev Hiemdel (passed)
  12. O Henry Smith (passed)
  13. Mathais d'Arras (passed)
  14. Martin Luther King Jr (passed)
  15. Downtown-James Brown (passed)
  16. Darren McCarty (passed)
  17. George De FunkMaster (passed)
  18. Kurtis B-Ball Basketball (passed)
  19. Ahmad el Jeffe (passed)

If you have basic names, there must be more than one up to five for the above code to work, that are similar to those that I used during testing, this code might be for you.

If you have any improvements, please let me know. I am just in the early stages (first few months of figuring out RegEx.

Thanks and good luck, Steve

Steve Kinzey
  • 373
  • 2
  • 9
9

First name would be

"([a-zA-Z]{3,30}\s*)+"

If you need the whole first name part to be shorter than 30 letters, you need to check that seperately, I think. The expression ".{3,30}" should do that.

Your last name requirements would translate into

"[a-zA-Z]{3,30}"

but you should check these. There are plenty of last names containing spaces.

Jens
  • 25,229
  • 9
  • 75
  • 117
8

I'm working on the app that validates International Passports (ICAO). We support only english characters. While most foreign national characters can be represented by a character in the Latin alphabet e.g. è by e, there are several national characters that require an extra letter to represent them such as the German umlaut which requires an ‘e’ to be added to the letter e.g. ä by ae.

This is the JavaScript Regex for the first and last names we use:

/^[a-zA-Z '.-]*$/

The max number of characters on the international passport is up to 31. We use maxlength="31" to better word error messages instead of including it in the regex.

Here is a snippet from our code in AngularJS 1.6 with form and error handling:

class PassportController {
  constructor() {
    this.details = {};
    // English letters, spaces and the following symbols ' - . are allowed
    // Max length determined by ng-maxlength for better error messaging
    this.nameRegex = /^[a-zA-Z '.-]*$/;
  }
}

angular.module('akyc', ['ngMessages'])
  .controller('PassportController', PassportController);
 
.has-error p[ng-message] {
  color: #bc111e;
}

.tip {
  color: #535f67;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
<script src="https://code.angularjs.org/1.6.6/angular-messages.min.js"></script>

<main ng-app="akyc" ng-controller="PassportController as $ctrl">
  <form name="$ctrl.form">

    <div name="lastName" ng-class="{ 'has-error': $ctrl.form.lastName.$invalid} ">
        <label for="pp-last-name">Surname</label>
        <div class="tip">Exactly as it appears on your passport</div>
        <div ng-messages="$ctrl.form.lastName.$error" ng-if="$ctrl.form.$submitted" id="last-name-error">
          <p ng-message="required">Please enter your last name</p>
          <p ng-message="maxlength">This field can be at most 31 characters long</p>
          <p ng-message="pattern">Only English letters, spaces and the following symbols ' - . are allowed</p>
        </div>
        
        <input type="text" id="pp-last-name" ng-model="$ctrl.details.lastName" name="lastName"
               class="form-control" required ng-pattern="$ctrl.nameRegex" ng-maxlength="31" aria-describedby="last-name-error" />
      </div>

      <button type="submit" class="btn btn-primary">Test</button>

  </form>
</main>
Alexander Burakevych
  • 2,396
  • 1
  • 24
  • 25
8

This regex work for me (was using in Angular 8) :

([a-zA-Z',.-]+( [a-zA-Z',.-]+)*){2,30}

enter image description here

It will be invalid if there is:-

  1. Any whitespace start or end of the name
  2. Got symbols e.g. @
  3. Less than 2 or more than 30

Example invalid First Name (whitespace)

enter image description here

Example valid First Name :

enter image description here

Snowbases
  • 2,316
  • 2
  • 20
  • 26
7

As maček said:

Don't forget about names like:

Mathias d'Arras

Martin Luther King, Jr.

Hector Sausage-Hausen

and to remove cases like:

..Mathias

Martin king, Jr.-

This will cover more cases:

^([a-z]+[,.]?[ ]?|[a-z]+['-]?)+$
Asim K T
  • 16,864
  • 10
  • 77
  • 99
7

Read almost all highly voted posts (only some are good). After understanding the problem in detail & doing research, here are the tight regexes:

1). ^[A-Z][a-z]*(([,.] |[ '-])[A-Za-z][a-z]*)*(\.?)$

  • name Z is allowed contrary to the assumption made by some in the thread.
  • No leading or trailing spaces are allowed, empty string is NOT allowed, string containing only spaces is NOT allowed
  • Supports English alphabets only
  • Supports hyphens (Some-Foobarbaz-name, Some foobarbaz-Name), apostrophes (David D'Costa, David D'costa, David D'costa R'Costa p'costa), periods (Dr. L. John, Robert Downey Jr., Md. K. P. Asif) and commas (Martin Luther, Jr.).
  • First alphabet of only the first word of a name MUST be capital.
    NOT Allowed: John sTeWaRT, JOHN STEWART, Md. KP Asif, John Stewart PhD
    Allowed: John Stewart, John stewart, Md. K P Asif
    you can easily modify this condition.

If you also want to allow names like Queen Elizabeth 2 or Henry IV:
2). ^[A-Z][a-z]*(([,.] |[ '-])[A-Za-z][a-z]*)*([.]?| (-----)| [1-9][0-9]*)$

replace ----- with roman numeral's regex (which itself is long) OR you can use this alternative regex which is based on KISS philosophy [IVXLCDM]+ (here I, V, X, ... in ANY random order will satisfy the regex).


I personally suggest to use this regex:
3). ^[A-Z][a-z]*(([,.] |[ '-])[A-Za-z][a-z]*)*(\.?)( [IVXLCDM]+)?$
Feel free to try this regex HERE & make any modifications of your choice.

I have provided with tight regex which covers every possible name I found on my research with no bug. Modify these regexes to relax some of the unwanted constraints.


[UPDATE - March, 2022]

Here are 4 more regexes:

^[A-Za-z]+(([,.] |[ '-])[A-Za-z]+)*([.,'-]?)$

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

^( ([A-Za-z,.'-]+|$))+|([A-Za-z,.'-]+( |$))+$

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

It's been a while since I looked back at these 4 regexes so I forgot their specifications. These 4 regexes are not tight, unlike the previous ones but do the job very well. These regexes distinguish 3 parts of a name: English alphabet, space and special character. Which one you need out of these 4 depends on your answer (Yes/No) to these questions:

  1. have at least 1 alphabet?
  2. can start with a space or a special character?
  3. can end with a space or a special character?
  4. are 2 consecutive spaces allowed?
  5. are 2 consecutive special characters allowed?

Note: name validation should ONLY serve as a warning NOT a necessity a name should fulfill because there is no fixed naming pattern, if there is one it can change overnight and thus, any tight regex you come across will become obsolete somewhere in future.

Aman Godara
  • 384
  • 1
  • 4
  • 6
  • `^[A-Za-z][a-z]*(([,.] |[ '-])[A-Za-z][a-z]*)*(\.?)( [IVXLCDM]+)?$` removes the constraint of first alphabet of the first word of a name to be capital. To further modify the regex to ignore case, replace both `[A-Za-z][a-z]*` with `[A-Za-z]+` – Aman Godara Dec 14 '21 at 05:31
  • This regex works only for English alphabets as mentioned in the post. – Aman Godara Feb 22 '22 at 11:14
  • My friend's last name is DeBiasi, which wouldn't be permitted by the regexes above. He lives in the US. – Christoph Mar 21 '22 at 18:42
  • 1
    @Christoph in that case you can make a slight modification to the regex to not take the case of an alphabet into account, right? But I certainly missed a case like "DeBiasi", never came across any name like this before. Thanks for pointing out. – Aman Godara Mar 22 '22 at 14:05
  • @Christoph I edited the answer to add 4 more regexes, but I forgot what these 4 regexes were supposed to do. See if they meet your purpose. – Aman Godara Mar 31 '22 at 09:54
6

There is one issue with the top voted answer here which recommends this regex:

/^[a-z ,.'-]+$/i

It takes spaces only as a valid name!

The best solution in my opinion is to add a negative look forward to the beginning:

/^(?!\s)([a-z ,.'-]+)$/i
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Rez.Net
  • 1,354
  • 2
  • 19
  • 28
4

I use:

/^(?:[\u00c0-\u01ffa-zA-Z'-]){2,}(?:\s[\u00c0-\u01ffa-zA-Z'-]{2,})+$/i

And test for maxlength using some other means

malix
  • 3,566
  • 1
  • 31
  • 41
4

I didn't find any answer helpful for me simply because users can pick a non-english name and simple regex are not helpful. In fact it's actually very hard to find the right expression that works for all languages.

Instead, I picked a different approach and negated all characters that should not be in the name for the valid match. Below pattern negates numerical, special characters, control characters and '\', '/'

Final regex without punctuations: ["] ['] [,] [.], etc. :

^([^\p{N}\p{S}\p{C}\p{P}]{2,20})$

with punctuations:

^([^\p{N}\p{S}\p{C}\\\/]{2,20})$

With this, all these names are valid:

alex junior
沐宸
Nick
Sarah's Jane ---> with punctuation support
ביממה
حقیقت
Виктория

And following names become invalid:

 Maria
k
١١١١١
123John

This means all names that don't have numerical characters, emojis, \ and are between 2-20 characters are allowed. You can edit the above regex if you want to add more characters to exclusion list.

To get more information about available patterns to include / exclude checkout this: https://www.regular-expressions.info/unicode.html#prop

Amir.n3t
  • 2,859
  • 3
  • 21
  • 28
3
^\p{L}{2,}$

^ asserts position at start of a line.

\p{L} matches any kind of letter from any language

{2,} Quantifier — Matches between 2 and unlimited times, as many times as possible, giving back as needed (greedy)

$ asserts position at the end of a line

So it should be a name in any language containing at least 2 letters(or symbols) without numbers or other characters.

4b0
  • 21,981
  • 30
  • 95
  • 142
2

If you are searching a simplest way, just check almost 2 words.

/^[^\s]+( [^\s]+)+$/

Valid names

  • John Doe
  • pedro alberto ch
  • Ar. Gen
  • Mathias d'Arras
  • Martin Luther King, Jr.

No valid names

  • John
  • 陳大文
pablorsk
  • 3,861
  • 1
  • 32
  • 37
-1

So, with customer we create this crazy regex:

(^$)|(^([^\-!#\$%&\(\)\*,\./:;\?@\[\\\]_\{\|\}¨ˇ“”€\+<=>§°\d\s¤®™©]| )+$)
-1

For first and last names theres are really only 2 things you should be looking for:

  1. Length
  2. Content

Here is my regular expression:

var regex = /^[A-Za-z-,]{3,20}?=.*\d)/

1. Length

Here the {3,20} constrains the length of the string to be between 3 and 20 characters.

2. Content

The information between the square brackets [A-Za-z] allows uppercase and lowercase characters. All subsequent symbols (-,.) are also allowed.

J Dorrian
  • 206
  • 3
  • 15