DD
DevDash

Regex for Alphanumeric Username

Regex Pattern

^[a-zA-Z0-9_]{3,16}$

Username with letters, digits, underscores (3-16 chars)

Quick Answer

The regex pattern for alphanumeric username is `^[a-zA-Z0-9_]{3,16}$`. Username with letters, digits, underscores (3-16 chars). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
john_doe✓ Matches
user123✓ Matches
alice_99✓ Matches
ab✗ No match
user@name✗ No match
toolongusernameover16chars✗ No match

Code Examples

javascript

const regex = /^[a-zA-Z0-9_]{3,16}$/;
const isValid = regex.test(value);

python

import re
pattern = r'^[a-zA-Z0-9_]{3,16}$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^[a-zA-Z0-9_]{3,16}$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^[a-zA-Z0-9_]{3,16}$/', $value)) {
    echo "valid";
}

java

String pattern = "^[a-zA-Z0-9_]{3,16}$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.