[SalesForce] Best REGEX for first/last name validation

Looking to stop people putting initials in the First / Last name fields, plus any special characters that you would not associate with a name. I've got something, although it is coming unstuck on names like McGowan or MacGowan. I understand why although I'm stumped to provide a solution.

This is what I have:

AND(
$User.ProfileId <> '00e30000001jDdz',
OR(
LEN(FirstName ) <=1,
MID(FirstName ,2,1) = " ",
NOT(
    REGEX(
          FirstName,
          '([A-Z][a-z]*)([\\s\\\'-][A-Z][a-z]*)*' 
         )
      )
   )
)

The UserProfile is to let a System Admin do what they want. The Len & Mid bits are to stop initials, or people putting in "S J ".

There is probably a more elegant way of doing this but I'm rather fresh to REGEX.

Any suggestions on how to better this?

Thanks,

Best Answer

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.

Related Topic