splitforms.com

Free tool · runs in your browser

Username regex tester

Test username length, allowed characters and separators. Pick a generic, GitHub, Instagram or Twitter/X style preset, paste usernames one per line, and check syntax and reserved names in your browser.

3-16 ASCII letters, digits, dots, underscores or hyphens. Must start and end with a letter or digit; consecutive separators are allowed.

5 of 10 pass

UsernameSyntaxResult
john_doePassPass
aFailFail
.johnFailFail
john..doePassPass
adminPassReserved
Jean-LucPassPass
user-FailFail
ab12PassPass
ROOTPassReserved
john__doePassPass

Reserved names are blocked case-insensitively, even when their syntax passes. This does not check whether an account exists.

HTML pattern is checked with the v flag in modern browsers; escape - inside character classes. Validate syntax, reserved names and uniqueness on the server.

Username rules and reserved names

The generic preset allows 3-16 ASCII letters, digits, dots, underscores and hyphens, with a letter or digit at each end. Choose no double separators to also reject adjacent punctuation. Platform-style presets use their own length and separator rules.

The tester separately blocks admin, root, support, api, www, null, undefined and system regardless of case. Regex cannot check account availability; enforce uniqueness on the server.

Recommended pattern

Default preset: ^[a-zA-Z0-9](?:[a-zA-Z0-9._-]{1,14})[a-zA-Z0-9]$. Requires 3-16 characters and a letter or digit at both ends. Reserved names are checked separately.

PresetRegexPass / fail examplesNotes
Generic (3-16 characters)^[a-zA-Z0-9](?:[a-zA-Z0-9._-]{1,14})[a-zA-Z0-9]$Pass: john_doe, Jean-Luc, ab12, john..doe
Fail: a, .john, user-, abcdefghijklmnopq
3-16 ASCII letters, digits, dots, underscores or hyphens. Must start and end with a letter or digit; consecutive separators are allowed.
No double separators (3-16 characters)^(?!.*[._-]{2})[a-zA-Z0-9](?:[a-zA-Z0-9._-]{1,14})[a-zA-Z0-9]$Pass: john_doe, Jean-Luc, ab12
Fail: john..doe, john__doe, john._doe, .john, user-
Generic rules plus a negative lookahead rejecting any two adjacent separators, including mixed pairs such as ._ or -_.
GitHub style (1-39 characters)^[a-zA-Z0-9](?:[a-zA-Z0-9]|-(?=[a-zA-Z0-9])){0,38}$Pass: a, Jean-Luc, ab12
Fail: john_doe, john.doe, john--doe, user-
1-39 letters or digits with single internal hyphens. No dots, underscores, leading or trailing hyphens. This tests syntax, not account availability.
Instagram style (1-30 characters)^(?!\.)(?!.*\.$)(?!.*\.\.)[a-zA-Z0-9._]{1,30}$Pass: a, john_doe, john.doe, _john_
Fail: .john, john., john..doe, Jean-Luc
1-30 letters, digits, dots or underscores. No leading, trailing or consecutive dots; underscores may repeat. Availability is checked separately.
Twitter/X style (4-15 characters)^[A-Za-z0-9_]{4,15}$Pass: john_doe, ab12, _user
Fail: abc, john.doe, Jean-Luc, abcdefghijklmnop
4-15 ASCII letters, digits or underscores. No dots or hyphens. This preset checks syntax only and does not query X.

HTML username input

username.html

<label for="username">Username</label>
<input id="username" name="username" type="text" autocomplete="username" autocapitalize="none" spellcheck="false" pattern="[a-zA-Z0-9](?:[a-zA-Z0-9._\-]{1,14})[a-zA-Z0-9]" title="3-16 letters, digits, dots, underscores or hyphens; start and end with a letter or digit" minlength="3" maxlength="16" required>

Use autocomplete="username" for autofill. The pattern attribute matches the whole value with the v flag; escape literal hyphens. Pair it with required. HTML pattern checks syntax; add reserved-name checks on the server.

Validate on the server too

validate-username.js

const USERNAME_RE = new RegExp("^[a-zA-Z0-9](?:[a-zA-Z0-9._-]{1,14})[a-zA-Z0-9]$");
const RESERVED = new Set(["admin", "root", "support", "api", "www", "null", "undefined", "system"]);
const ok = USERNAME_RE.test(value) && !RESERVED.has(value.toLowerCase());
validate-username.php

$username = $_POST['username'] ?? '';
$reserved = ['admin', 'root', 'support', 'api', 'www', 'null', 'undefined', 'system'];
$ok = is_string($username)
    && preg_match('/\A[a-zA-Z0-9][a-zA-Z0-9._-]{1,14}[a-zA-Z0-9]\z/', $username) === 1
    && !in_array(strtolower($username), $reserved, true);
validate-username.py

import re

USERNAME_RE = re.compile(r"[a-zA-Z0-9][a-zA-Z0-9._-]{1,14}[a-zA-Z0-9]")
RESERVED = {"admin", "root", "support", "api", "www", "null", "undefined", "system"}
ok = USERNAME_RE.fullmatch(username) is not None and username.lower() not in RESERVED

These examples use the generic preset plus reserved-name checks. Use full-string matching and enforce a unique database constraint for account creation.

Username regex FAQ

What is a good regex for usernames?

For a 3-16 character ASCII username, use ^[a-zA-Z0-9](?:[a-zA-Z0-9._-]{1,14})[a-zA-Z0-9]$. It allows letters, digits, dots, underscores and hyphens, but requires a letter or digit at both ends. Check reserved names and uniqueness separately.

How do I stop consecutive dots or underscores?

Choose the no double separators preset. Its (?!.*[._-]{2}) lookahead rejects consecutive dots, underscores or hyphens, including mixed pairs. The generic preset permits consecutive separators.

Why does my pattern attribute with a hyphen fail?

Modern browsers compile HTML pattern with the v flag. Escape a literal hyphen as \- inside character classes; an invalid pattern can cause the browser to ignore the constraint. The HTML snippets here escape hyphens and omit anchors because pattern matches the entire value.

Should usernames be case-insensitive?

Choose one policy and enforce it consistently. These presets accept both uppercase and lowercase letters, and the reserved-name check is case-insensitive. If accounts are case-insensitive, normalize the lookup key on the server and enforce a unique database constraint while preserving display casing if desired.

Does a valid username mean it is available?

No. Regex checks syntax only. This tool separately blocks admin, root, support, api, www, null, undefined and system. Platform-style presets do not contact GitHub, Instagram or X, and do not guarantee account availability or acceptance.

Do I need to validate usernames on the server?

Yes. Browser validation can be bypassed. Repeat syntax, length and reserved-name checks on the server, then enforce uniqueness in the database. Use full-string matching, and decide explicitly whether to reject or trim surrounding whitespace.