Advice on How to Validate Names and Surnames Using Regex

Advice on how to validate names and surnames using regex

The correct way to validate names is:

  1. Accept any and all possible Unicode characters.
  2. Reject inputs of length 0.
  3. Make sure the result has at least one character that is neither of type \pM, \pZ, nor \pC; i.e., is neither one of the combining marks, separators, nor other (usually control) characters. A character class like [^\pM\pC\pZ] should suffice.

There: easy as can be. You will probably need Ruby 1.9 to do this properly, though.

Regular expression for validating names and surnames?

I'll try to give a proper answer myself:

The only punctuations that should be allowed in a name are full stop, apostrophe and hyphen. I haven't seen any other case in the list of corner cases.

Regarding numbers, there's only one case with an 8. I think I can safely disallow that.

Regarding letters, any letter is valid.

I also want to include space.

This would sum up to this regex:

^[\p{L} \.'\-]+$

This presents one problem, i.e. the apostrophe can be used as an attack vector. It should be encoded.

So the validation code should be something like this (untested):

var name = nameParam.Trim();
if (!Regex.IsMatch(name, "^[\p{L} \.\-]+$"))
throw new ArgumentException("nameParam");
name = name.Replace("'", "'"); //' does not work in IE

Can anyone think of a reason why a name should not pass this test or a XSS or SQL Injection that could pass?


complete tested solution

using System;
using System.Text.RegularExpressions;

namespace test
{
class MainClass
{
public static void Main(string[] args)
{
var names = new string[]{"Hello World",
"John",
"João",
"タロウ",
"やまだ",
"山田",
"先生",
"мыхаыл",
"Θεοκλεια",
"आकाङ्क्षा",
"علاء الدين",
"אַבְרָהָם",
"മലയാളം",
"상",
"D'Addario",
"John-Doe",
"P.A.M.",
"' --",
"<xss>",
"\""
};
foreach (var nameParam in names)
{
Console.Write(nameParam+" ");
var name = nameParam.Trim();
if (!Regex.IsMatch(name, @"^[\p{L}\p{M}' \.\-]+$"))
{
Console.WriteLine("fail");
continue;
}
name = name.Replace("'", "'");
Console.WriteLine(name);
}
}
}
}

Using RegEx for validating First and Last names in Java

Use this:

^[\p{L}\s.’\-,]+$

Demo: https://regex101.com/r/dQ8fK8/1

Explanation:

  1. The biggest problem you have is ' and is different. You can only achieve that character by copy pasting from the text.
  2. Use \- instead of - in [] since it will be mistaken as a range. For example: [a-z]
  3. You can use \s instead of for matching any whitespaces.

Email Regex Validation for Name and Surname

This regex will do it (including diacritics):

/^(([().!#$%&'*+\/=?^_`{|}~\u00BF-\u1FFF\u2C00-\uD7FF\w-]+[,\n#\s$&:\n\t]+){2,}){0,}(?:<){0,1}[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]))(?:>){0,1}$/igm

The following line checks for names, including diacritics \u00BF-\u1FFF\u2C00-\uD7FF\w- (not the e-mail). It is wrapped in a capturing group which evaluates the if there are any names. If there are names A second capturing group within checks if there are at least two {2,}. It's a compressed version since we don't need to discriminate between a first and last name.

The <> are enclosed in non-capturing groups and should occur zero or one time.

/(([().!#$%&'*+\/=?^_`{|}~\u00BF-\u1FFF\u2C00-\uD7FF\w-]+[,\n#\s$&:\n\t]+){2,}){0,}/

const regEx = /^(([().!#$%&'*+\/=?^_`{|}~\u00BF-\u1FFF\u2C00-\uD7FF\w-]+[,\n#\s$&:\n\t]+){2,}){0,}(?:<){0,1}[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]))(?:>){0,1}$/ig;const emails = document.querySelectorAll("div");
emails.forEach(element => { if (element.textContent.match(regEx)) { element.classList.add("match"); }});
div.match {  color: #ff0000;}
<div>test@test.com</div><div><test@test.com></div><div>Näme Surmame <test@test.com></div><div>Name, Surname <test@test.com></div><div>Name 北京市 Surname surname2 Surname3 <test@test.com></div><div>Name (name 2), Surname-surname2 <test@test.com></div><div>Name <test@test.com></div><div>test @test.com</div><div>isthis3avalid test <test@test></div></textarea>

How to match person name and surname separately using Regex?

If you're really just interested in getting the first two names, there's nothing wrong with repeating the capture group with a non-captured space in the middle.

^([A-Za-z]+) ([A-Za-z]+)+

Sample Image

Alternatively, complementing @Wiktor Stribiżew's answer to exclude spaces in the beginning, you could do:

^\s*([A-Za-z ]+) ([A-Za-z]+)$

Sample Image

Regex for names

  • Hyphenated Names (Worthington-Smythe)

Add a - into the second character class. The easiest way to do that is to add it at the start so that it can't possibly be interpreted as a range modifier (as in a-z).

^[A-Z][-a-zA-Z]+$
  • Names with Apostophies (D'Angelo)

A naive way of doing this would be as above, giving:

^[A-Z][-'a-zA-Z]+$

Don't forget you may need to escape it inside the string! A 'better' way, given your example might be:

^[A-Z]'?[-a-zA-Z]+$

Which will allow a possible single apostrophe in the second position.

  • Names with Spaces (Van der Humpton) - capitals in the middle which may or may not be required is way beyond my interest at this stage.

Here I'd be tempted to just do our naive way again:

^[A-Z]'?[- a-zA-Z]+$

A potentially better way might be:

^[A-Z]'?[- a-zA-Z]( [a-zA-Z])*$

Which looks for extra words at the end. This probably isn't a good idea if you're trying to match names in a body of extra text, but then again, the original wouldn't have done that well either.

  • Joint Names (Ben & Jerry)

At this point you're not looking at single names anymore?

Anyway, as you can see, regexes have a habit of growing very quickly...



Related Topics



Leave a reply



Submit