-1

Reading at this: regular expression for first and last name

Here is my test cases:

    Fail cases:
    陳大文 (fail, not support international name)
    Stev3 Smith (fail, contain number)
    123 (fail, contain number)
    @##$ smith (fail, special char)
    .Mathias (fail, dot)
    ..Mathias (fail, dot dot)
    Success cases:
    Martin Luther King, Jr. (pass)
      john   smith (pass, javascript does str.trim())
    d'Are to Beaware (pass)
    Jo Blow (pass)
    Steve Johnson-Smith (pass)
    O Henry Smith (pass)
    john & john (pass, support &)

My current solution:

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

My issue is that I cannot pass this:

.Mathias (fail, dot)
..Mathias (fail, dot dot)

and this at the same time

Martin Luther King, Jr.

Any idea?

kenpeter
  • 7,404
  • 14
  • 64
  • 95

2 Answers2

1

The problem with wanting to do this kind of thing is that you can only approach this issue going by what you think you know about names. There are certainly many people who have the kinds of names that you're used to seeing, like John Smith, but there are also many who do not.

For example, you want to fail people who have numbers in their names but there are people who have numbers in their name because it's not illegal everywhere. There people in the Spanish speaking regions of the world who have unusually long full names and multiple middle names, that could potentially break other assumptions you make about names too if you're not familiar with it.

It might seem a bit silly to throw away all validation because of a couple people with numbers in their names, but the real problem is that you're trying to make assumptions about an unknown unknown. You don't know what kinds of names that are out there and you also don't know that there are things you don't know about names. This blog post about falsehoods programmers believe about names highlights this issue more in-depth.

As long as you're making sure to santize names to keep all the Bobby Tables of the world out of your database, there's really no comprehensive way to do name validation without making really bad guesses. Especially not with regex.

Xetera
  • 1,390
  • 1
  • 10
  • 17
0

Simple, just replace [\w'\-,.] (which will match a .) with \w, which won't.

Your test cases will pass - whether or not it's a 'real' name is still up to debate.

Rubydesic
  • 3,386
  • 12
  • 27