Regex Pattern

Username Validation Regex (Alphanumeric + Underscore)

By Samrat•Last updated: Nov 2026
Regex Pattern
^[a-zA-Z0-9_]{3,16}$

Quick answer

This regex ensures a username is between 3 and 16 characters long, and only contains letters (a-z, A-Z), numbers (0-9), and underscores (_).

🛠️ Fix it right here with AI Regex Generator

Need to modify this pattern or generate a new one? Edit the regex below and let our AI help.

AI suggestions can be wrong. Review and test before applying.

Live Regex Tester

Type a string below to see if it matches this pattern.

How it works

The ^[a-zA-Z0-9_] part defines the allowed characters. The {3,16} defines the length constraints. The $ ensures the entire string matches.

Code Examples

✅ Correct Usage

// JavaScript
const usernameRegex = /^[a-zA-Z0-9_]{3,16}$/;
console.log(usernameRegex.test("samrat_dev")); // true
console.log(usernameRegex.test("sam rat")); // false (contains space)

❌ What NOT to do

// BAD: Allows spaces and special characters
const badRegex = /^.{3,16}$/;

Frequently asked questions

How do I allow dots (.) as well?

Add a dot inside the brackets: /^[a-zA-Z0-9_.]{3,16}$/.

How do I prevent usernames from starting with a number?

Change the first bracket to require a letter: /^[a-zA-Z][a-zA-Z0-9_]{2,15}$/.

Related Search Terms

username regexregex for usernamealphanumeric regexusername validation

Related Regex Patterns

Need a custom regex pattern?

Use the free to describe what you need in plain English.

Want an AI with memory for your project?

MonsterX AI remembers your codebase across chats.