Validation
Username Regex
A username rule should be easy to explain on a sign-up form. This pattern requires the first character to be an ASCII letter, which avoids names that look like numbers, and then allows two to nineteen more letters, digits or underscores, giving a total length of three to twenty characters. The caret and dollar anchors make sure the whole input follows the rule, not just part of it. Dashes, dots and spaces are rejected, which keeps names safe to use in URLs and mentions. Adjust the numbers in the quantifier if your product needs longer or shorter names.
Pattern (JavaScript)
/^[a-zA-Z][a-zA-Z0-9_]{2,19}$/Starts at the start of the line/string, then a letter, then one of the characters a-zA-Z0-9_, between 2 and 19 times, then the end of the line/string.
Examples
Matches
- ✓ ali_test
- ✓ SaraDemo1
- ✓ abc
Does not match
- ✗ 1ali
- ✗ ab
- ✗ ali-test
Try it
0 matches (JavaScript engine)
ali_test
SaraDemo1
abc
1ali
ab
ali-testCode in 8 languages
Converted automatically from the JavaScript pattern; language-specific notes are shown where the syntax or support differs.
JavaScript
const re = /^[a-zA-Z][a-zA-Z0-9_]{2,19}$/g;
const text = "ali_test";
const matches = [...text.matchAll(re)].map((m) => m[0]);
console.log(matches);Python
import re
pattern = re.compile(r'^[a-zA-Z][a-zA-Z0-9_]{2,19}$')
text = 'ali_test'
matches = [m.group(0) for m in pattern.finditer(text)]
print(matches)PHP
<?php
$pattern = '/^[a-zA-Z][a-zA-Z0-9_]{2,19}$/';
$text = 'ali_test';
preg_match_all($pattern, $text, $matches);
print_r($matches[0]);Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]{2,19}$");
String text = "ali_test";
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_]{2,19}$`)
text := "ali_test"
fmt.Println(re.FindAllString(text, -1))
}C#
using System;
using System.Text.RegularExpressions;
var pattern = new Regex(@"^[a-zA-Z][a-zA-Z0-9_]{2,19}$");
var text = "ali_test";
foreach (Match m in pattern.Matches(text))
{
Console.WriteLine(m.Value);
}Rust
// Cargo.toml: regex = "1"
use regex::Regex;
fn main() {
let re = Regex::new(r"^[a-zA-Z][a-zA-Z0-9_]{2,19}$").unwrap();
let text = "ali_test";
for m in re.find_iter(text) {
println!("{}", m.as_str());
}
}- Add regex = "1" under [dependencies] in Cargo.toml.
Ruby
pattern = /^[a-zA-Z][a-zA-Z0-9_]{2,19}$/
text = 'ali_test'
matches = text.to_enum(:scan, pattern).map { Regexp.last_match[0] }
p matchesFrequently asked questions
- Why must a username start with a letter?
- Names that start with digits can be confused with user IDs or numbers in URLs. Requiring a letter first is a simple way to avoid that.
- How do I allow dots in usernames?
- Add a dot to the class, for example [a-zA-Z0-9_.]. Consider also blocking two dots in a row, which needs a separate check.
- Does this make usernames case-insensitive?
- No. The pattern only checks characters. Store a lowercase copy of the name if you want Ali_Test and ali_test to count as the same user.