Username
Validate usernames that are 3–16 characters long and contain only letters, digits, underscores, and hyphens.
Try it in RegexPro →Available in
JavaScript / ECMAScript code
const re = new RegExp("^[a-zA-Z0-9_\\-]{3,16}$", "");
const input = "john_doe";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
import re
pattern = re.compile(r"^[a-zA-Z0-9_\-]{3,16}$")
input_text = "john_doe"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-zA-Z0-9_\-]{3,16}$`)
input := `john_doe`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
^[a-zA-Z0-9_\-]{3,16}$Raw source: ^[a-zA-Z0-9_\-]{3,16}$
How it works
Examples
Input
john_doeMatches
john_doe
Input
user-123Matches
user-123
Input
abNo match
—Common use cases
- •User registration form validation
- •API username parameter sanitization
- •Social platform handle creation
- •Database username format enforcement
Related patterns
Alphanumeric Only
ValidationMatch strings containing only letters (A–Z, a–z) and digits (0–9), with no spaces or special characters.
Twitter / X Handle
ValidationMatch Twitter/X @handles — 1 to 15 characters of letters, digits, or underscores preceded by @.
Password (No Special Chars)
ValidationValidate passwords requiring at least one lowercase, one uppercase, one digit, and minimum 8 characters — no special characters required.
IBAN (International Bank Account Number)
ValidationValidate IBAN bank account identifiers: 2-letter country code, 2 check digits, 11–30 alphanumerics.