Regular expression for name validation
How regex name validation works, why ASCII-only patterns reject real names, a Unicode-aware pattern with test cases, and when to skip regex.
A regular expression for name validation decides which strings a form or API will accept as a person's name. The pattern does not change the name. It only accepts or rejects it. The real question is whether the pattern rejects names that real people have. This review covers what a name regex checks, where common patterns fail, a more permissive alternative, and when to use minimal checks instead.
Short recommendation
For most sign-up, checkout and contact forms, use light validation. Trim the input, require it to be nonempty, cap the length to what your storage can hold, and reject control characters. Use a stricter regular expression only when a downstream system actually needs a limited character set. If you do, use a Unicode-aware pattern rather than an ASCII-only one. The benefit of a regex is predictable input for systems that cannot handle arbitrary text. The cost is that every rule you add will reject some real names. If nothing downstream needs restriction, a plain nonempty text field is the better choice.
What a name regex does and does not change
A validation pattern only tests the string and returns match or no match. Any change to the stored characters comes from separate steps you run before or after the test: trimming whitespace, Unicode normalization, case folding, or replacing curly apostrophes with straight ones. Keep those steps separate in your code and documentation, because each one changes what you store.
Display direction is also separate from validation. Arabic and Hebrew names are stored in logical order and shown right to left by the renderer. A regex sees the stored order, so bidirectional display has no effect on whether a pattern matches. Invisible formatting characters are a different matter. Marks such as the left-to-right mark or the right-to-left override are real characters in the string, and a letters-only pattern will reject them.
Why common patterns reject real names
- ^[A-Za-z]+$ rejects José, Zoë, Müller, O'Brien, Mary-Jane, and any two-part name with a space.
- So does any non-Latin name: 李小龙, Ελένη, محمد.
- The Cyrillic range [А-Яа-я] looks complete but misses Ё and ё, which are encoded outside that range. Names like Ёлкин or Алёна fail unless you add them.
- Requiring both a first and a last name rejects people who have one name.
- A minimum length of two or three characters rejects valid short names.
- Python's [^\W\d_] does accept letters in any script. It does not accept combining marks, though, so names in scripts that rely on them, such as Devanagari, can fail. Names typed in decomposed form fail too, such as e followed by a combining accent.
A more permissive Unicode-aware pattern
This pattern accepts letters and combining marks from any script. Letter runs can be separated by spaces, straight or curly apostrophes, hyphens, or periods, and an optional final period is allowed for forms like Jr. In JavaScript, the u flag is required for \p{…} property escapes.
const NAME = /^[\p{L}\p{M}]+(?:[ '’.-]+[\p{L}\p{M}]+)*\.?$/u;
function isPlausibleName(input) {
const s = input.normalize("NFC").trim();
return s.length > 0 && s.length <= 200 && NAME.test(s);
}
Python's built-in re module does not support \p{L}. Use the third-party regex module instead. Prefer fullmatch over ^…$, because in Python $ also matches just before a trailing newline. The length cap of 200 in both examples is a placeholder. Set it from your actual storage limit.
import unicodedata
import regex
NAME = regex.compile(r"[\p{L}\p{M}]+(?:[ '’.-]+[\p{L}\p{M}]+)*\.?")
def is_plausible_name(value: str) -> bool:
s = unicodedata.normalize("NFC", value).strip()
return 0 < len(s) <= 200 and NAME.fullmatch(s) is not None
Java, .NET and PCRE in UTF mode also support \p{L} and \p{M}. Check your engine's documentation for flag requirements before reusing the pattern.
Expected results for sample inputs
These results are worked out from the pattern's rules, after NFC normalization and trimming. They have not been run against every engine, so test them in yours.
| Input | Result | Reason |
| José | Accepted | é is a letter; a decomposed accent is also covered by \p{M} |
| O'Brien / O’Brien | Accepted | Both apostrophe forms are in the separator set |
| Mary-Jane Ёлкина | Accepted | Hyphen and space separators; Ё is a letter |
| J. R. R. Tolkien | Accepted | Period-plus-space separators are allowed |
| John Smith Jr. | Accepted | Optional trailing period |
| 李小龙 | Accepted | CJK characters are letters (category Lo) |
| Anna2 | Rejected | Digits are not allowed |
| Name with a no-break space (U+00A0) | Rejected | Only an ordinary space is in the separator set |
| Persian name containing a zero-width non-joiner (U+200C) | Rejected | The joiner is a format character, not a letter or mark |
| 𝐉𝐨𝐡𝐧 (mathematical bold letters) | Accepted | These symbols are classified as letters |
The last three rows need decisions. If you serve Persian-speaking users, add U+200C to the allowed characters inside a name. If users paste names from documents, convert no-break spaces to ordinary spaces before testing. If stylized letters should not be stored, add a separate check for them, because a letter-based pattern will let them through.
Copying, pasting and stylized "font" text
Names pasted from word processors, PDFs or chat apps often carry characters you cannot see: curly apostrophes, no-break spaces, zero-width characters, or direction marks. Without a normalization step, a correct-looking name can fail validation and the user cannot tell why. Normalize the input to NFC, trim it, and convert known whitespace variants before testing.
Stylized "font generator" text is not a font. It replaces ordinary letters with different Unicode characters, such as the mathematical bold letters in the table above. A stored name made of these characters will not match a search for the plain spelling. It may also be read aloud or sorted differently from ordinary letters. How a given screen reader handles these characters was not tested for this article, so do not assume one result applies everywhere. If you need a plain-text equivalent, NFKC normalization maps many of these characters to ordinary letters. NFKC also rewrites other compatibility characters, so it is usually safer for comparison and search keys than for the value you store and display.
Accessibility and error handling
When a name is rejected, say which character or rule caused it, and offer a fix where possible. This follows the WCAG success criteria for error identification and error suggestion. A generic "invalid name" message gives a person whose name was wrongly rejected no way forward. If your validation is strict for a downstream reason, consider offering a second free-text field for the name as the person writes it.
What a name regex is not for
Name validation does not protect against SQL injection or cross-site scripting. Apostrophes are legitimate in names, so they must be handled safely anyway. Use parameterized queries and context-appropriate output escaping regardless of what the pattern allows. A regex also cannot tell you whether a name is real. It can only tell you whether the string fits the character rules you chose.
Gaps in this review
No tests were supplied for specific form builders, databases, browsers or assistive technologies. The sample results above follow from the pattern's documented rules, not from measured runs. Before deploying, test with names from the languages your users actually write, in the engine and storage you actually use.