Name Validation with Regular Expressions

How regex name validation works, why strict patterns reject real names, how Unicode and copy-paste affect results, and when a simple check works better.

Validating a name field with a regular expression looks simple until real names arrive: José, O’Brien, Mary-Jane, Nguyễn, 李小龙, or a single-word name with no surname. This review covers what a name regex does and does not change, where strict patterns fail, and when a minimal check is the better choice.

Evidence note: no test results, measurements or tool-specific documentation were supplied for this article. The behavior described here comes from general, documented properties of common regex engines and Unicode. Confirm it in your own engine, framework and target browsers before you rely on it.

Is regex name validation suitable for your use?

Recommendation: use a regular expression on a name field only as a loose plausibility check. It can catch obvious mistakes such as an empty field, a pasted email address or stray markup. Do not use it to decide what counts as a valid human name.

The main benefit is fast, consistent feedback on clearly malformed input. The main tradeoff is that every rule you add excludes some real people, and the person filling in the form usually cannot work around a rejection of their own legal name.

In some cases, a plain-text check with no character rules is the better choice: trim the input, require it to be non-empty, enforce a length limit that fits your storage, and accept anything else. This suits international audiences, legal or medical records where the name must match an official document, and any form where a false rejection costs more than an odd-looking entry. Escaping on output and parameterized queries protect against injection. A name regex does not.

What a name regex actually changes

A regex test does not change the input. It returns match or no match, and the stored characters stay the same. Changes to the stored value only happen when code around the regex alters the string, for example by trimming whitespace, collapsing spaces, changing case or applying Unicode normalization. Treat these as separate decisions, because each one alters what you store.

The phrase 'name regex' has two meanings. This article is about validating a person’s name. In regex syntax, 'named groups' refers to labeled capture groups such as (?<first>...), which is unrelated. Searching free text for proper names is a third task. Patterns based on capital letters are unreliable for it, because sentence starts, titles and acronyms are capitalized too, and many scripts have no letter case.

Why ASCII-only patterns fail

A common first attempt accepts only English letters:

^[A-Za-z]+$

This rejects accented letters (José), apostrophes (O’Brien), hyphens (Mary-Jane), spaces (Mary Jane), Vietnamese diacritics (Nguyễn) and non-Latin scripts (李小龙). Adding a space and hyphen fixes some cases, but anything limited to A–Z still excludes most of the world’s writing systems.

A Unicode-aware, permissive pattern

Engines that support Unicode property escapes can match any letter with \p{L} and any combining mark with \p{M}. These are available in JavaScript with the u or v flag, Java, .NET, PCRE and Python’s third-party regex module. Python’s built-in re module does not support \p{...}.

// Must start with a letter or mark; may contain spaces, apostrophes,
// periods and hyphens; must end with a letter, mark or period.
const NAME_PATTERN = /^[\p{L}\p{M}](?:[\p{L}\p{M}'’ .\-]*[\p{L}\p{M}.])?$/u;

// MAX_NAME_LENGTH is a placeholder: set it to fit your storage.
function isPlausibleName(input) {
  const name = input.normalize("NFC").trim();
  return name.length > 0 && name.length <= MAX_NAME_LENGTH && NAME_PATTERN.test(name);
}
import regex  # third-party module; the standard re module lacks \p{...}

NAME_PATTERN = regex.compile(r"^[\p{L}\p{M}](?:[\p{L}\p{M}'’ .\-]*[\p{L}\p{M}.])?$")

This pattern accepts single-letter and single-word names, which is intentional. It still rejects some real names: names containing digits, the Catalan middle dot (as in Gil·la), and any punctuation not in the class. Add characters as your audience requires, and test the pattern against a list of names from the people you actually serve.

Several Unicode characters fall into letter categories, so \p{L} already covers them. For example, the Hawaiian ʻokina (U+02BB) is categorized as a modifier letter. The class above accepts both the straight apostrophe and the typographic right single quotation mark (’), because phones and word processors often substitute one for the other.

Combining marks and normalization

An accented letter such as é can be stored as one code point (U+00E9) or as e followed by a combining acute accent (U+0301). The two forms look identical but are different strings. A pattern without \p{M} accepts the first form and rejects the second. Normalizing to NFC before testing turns most such sequences into single code points, but not all. Some letter-plus-mark combinations have no precomposed form, so keep \p{M} in the pattern.

Normalization changes stored characters. If you need to preserve exactly what the user typed, store the original separately and use the normalized form only for validation and comparison.

In Python’s built-in re module, the class [^\W\d_] matches Unicode letters in str patterns, but it does not match combining marks. Normalizing to NFC first helps but does not cover every case. The regex module with \p{M} is more reliable.

Copy-paste, accessibility and compatibility

Pasted names often carry characters the user cannot see, such as leading or trailing spaces, non-breaking spaces (U+00A0) and zero-width characters. The pattern above allows only an ordinary space, so a name containing a non-breaking space fails even though it looks correct. You can either replace whitespace runs with a single space before testing, which changes the stored value, or show an error message that points to the specific problem. Silent rejection of a correct-looking name is the worst outcome.

Length checks are also affected. In JavaScript, string length counts UTF-16 code units, not visible characters. Some characters count as two units, and a base letter plus combining marks counts as several. Set limits generously, or count grapheme clusters, for example with Intl.Segmenter where it is available, if the limit is shown to users.

For accessibility, tie any validation message to the field programmatically, for example with aria-describedby, so screen reader users hear it. The message should say what was rejected, not only that the input is invalid. Since no assistive-technology testing was supplied for this article, verify the behavior with the screen readers your audience uses.

The HTML pattern attribute is compiled as a JavaScript regex. The current HTML specification uses the v flag, which is stricter than u about unescaped characters inside classes, such as a literal hyphen. Older browser versions used the u flag. The pattern above escapes the hyphen for this reason. Test it in your target browsers, and always repeat validation on the server, because client-side checks can be bypassed.

Approaches compared

Approach | What it accepts | Main risk | When to use it
ASCII letters only (^[A-Za-z]+$) | Unaccented Latin letters | Rejects most real names worldwide | Rarely appropriate for names
ASCII plus space, hyphen, apostrophe | Many English-language names | Still rejects accents and non-Latin scripts | Only for legacy systems that cannot store Unicode
Unicode letters and marks plus limited punctuation | Most names in most scripts | Rejects digits and rare punctuation unless added | General-purpose forms with a light plausibility check
Non-empty plus length limit, no character rules | Any text | Accepts junk entries | Legal, medical or international forms where false rejection is costly

Summary

  • Use regex on name fields to catch clear mistakes, not to define what a valid name is.
  • Prefer Unicode property classes (\p{L}, \p{M}) over A–Z ranges, and include both apostrophe forms.
  • Treat trimming, whitespace collapsing and normalization as deliberate changes to stored data.
  • Explain rejections clearly and accessibly, and validate again on the server.
  • If false rejections would be costly, drop character rules and keep only a non-empty and length check.