splitforms.com

Free tool · runs in your browser

Name regex tester

Test full-name patterns for O'Brien, Jean-Luc, accents and Unicode letters. Pick a preset, edit the regex, paste names one per line, then copy JavaScript or an HTML pattern snippet.

Unicode letters (\p{L}) and marks (\p{M}); apostrophe, space, dot and hyphen after the first letter. Single-character and single-word names are allowed. Use the u flag in JavaScript and PHP.

8 of 12 pass

  • PassO'Brien
  • PassJean-Luc
  • PassJosé María
  • PassZoë
  • PassNguyễn
  • Pass李小龙
  • FailX Æ A-12
  • PassMr. T
  • Fail
  • Faila1
  • Fail<script>
  • PassA

HTML pattern is checked with the v flag in modern browsers; escape - inside character classes. Trim and length-limit on the server.

Why strict name regex is a bad idea

Names vary by culture, language and legal ID. A single ASCII regex cannot represent personal names around the world. Use regex only to catch obvious typos, then trim and length-limit instead of over-validating (for example maxlength="100" and required).

Recommended pattern

Default preset: ^[\p{L}\p{M}][\p{L}\p{M}' .-]*$ with new RegExp(..., "u"). Allows one-character names, single-word names, apostrophes and hyphens.

PresetRegexPass / fail examplesNotes
Permissive (recommended)^[\p{L}\p{M}][\p{L}\p{M}' .-]*$Pass: O'Brien, Jean-Luc, José María, Zoë, Nguyễn, 李小龙, Mr. T, A, José
Fail: X Æ A-12, , a1, <script>
Unicode letters (\p{L}) and marks (\p{M}); apostrophe, space, dot and hyphen after the first letter. Single-character and single-word names are allowed. Use the u flag in JavaScript and PHP.
Latin letters + accents^[A-Za-zÀ-ÖØ-öø-ÿ' -]+$Pass: O'Brien, Jean-Luc, José María, Zoë
Fail: Mr. T, Nguyễn, 李小龙, X Æ A-12, a1, <script>
Western letters and common accented Latin ranges only. CJK and other scripts fail. Trim whitespace before matching; a spaces-only value can match this pattern.
First + last name required^[\p{L}\p{M}][\p{L}\p{M}' .-]* [\p{L}\p{M}][\p{L}\p{M}' .-]*$Pass: José María, Mary Jane, Jean Luc
Fail: O'Brien, Jean-Luc, 李小龙, A, , a1, <script>
At least one ASCII space between two name parts. Hyphenated single tokens such as Jean-Luc do not count as two names.
ASCII only (strict, not recommended)^[A-Za-z][A-Za-z' \-]*$Pass: O'Brien, Jean-Luc, A
Fail: Mr. T, José, Zoë, Nguyễn, 李小龙, José María, X Æ A-12, a1, <script>
A–Z only. Rejects most real names outside basic English. Prefer the permissive Unicode pattern instead.

HTML name input

name.html

<label for="name">Full name</label>
<input id="name" name="name" type="text" autocomplete="name" pattern="[\p{L}\p{M}][\p{L}\p{M}' .\-]*" title="Letters; apostrophe, space, dot or hyphen allowed after the first letter" maxlength="100" required>

Use autocomplete="name" for autofill and the pattern attribute without ^ or $ (the browser anchors the whole value). Pair with required and maxlength.

Validate on the server too

validate-name.js

const NAME_RE = new RegExp("^[\\p{L}\\p{M}][\\p{L}\\p{M}' .-]*$", "u");
const ok = NAME_RE.test(value.trim());
validate-name.php

$name = trim($_POST['name'] ?? '');
$ok = preg_match('/^[\p{L}\p{M}][\p{L}\p{M}\' .-]*$/u', $name) === 1;
validate-name.py

# pip install regex  (\p{L} support)
import regex

NAME_RE = regex.compile(r"[\p{L}\p{M}][\p{L}\p{M}' .-]*")
ok = NAME_RE.fullmatch(name.strip()) is not None

Always trim() first, enforce a generous max length, and treat regex as a hint—not proof of a real person.

Name regex FAQ

What regex validates a person’s name with an apostrophe?

Use ^[\p{L}\p{M}][\p{L}\p{M}' .-]*$ with the u flag so O'Brien and D'Angelo pass. The first character must be a letter or mark; later characters may include apostrophe, space, dot or hyphen.

Should I require first and last name in the regex?

Usually no. Many people use one name, initials or multiple family names. If you need two parts for shipping labels, use a separate field or a looser rule (at least one space) and still allow overrides.

Does HTML pattern support \p{L} Unicode letters?

Modern browsers compile pattern with the v flag and accept \p{L} and \p{M}. Escape hyphens inside classes. Older browsers may ignore pattern; always validate again on the server.

Why is a strict ASCII name regex a bad idea?

Real names use accents (José, Zoë), scripts beyond A–Z (李小龙) and punctuation cultures expect (hyphens, apostrophes). Over-tight regex rejects paying customers; trim, maxlength and human review catch abuse better.

Can I block XSS with a name regex?

No. <script> should fail because < is not allowed, but regex is not a security boundary. Escape on output, use CSP and validate length; never rely on pattern alone for safety.