Auto-Generated Usernames at Signup: Rules That Actually Work
Every signup form faces the same question: what happens when the user leaves the username field empty? You can block submission, you can ask for an email-first flow, or you can generate a username automatically. The third option is usually the right one — provided your generator follows a few rules. Get them wrong and you will ship usernames your users cannot type, URLs your router cannot resolve, and collisions your code cannot recover from.
First, decide what a username is for
A username is an identifier, not a profile name. That distinction matters. The identifier must be unique, stable, and safe to appear in a URL like https://example.com/@jane_doe2847. The display name can be anything: unicode, spaces, emojis, changed at will. Teams that merge the two end up with support tickets like "I changed my display name and now my profile is a 404." Store both fields separately from day one. Let the user edit the display name freely; gate the identifier behind strict rules or keep it fully system-generated.
Rule 1: ASCII only, lowercase only
Restrict generated usernames to [a-z0-9_] or [a-z0-9-], 3 to 30 characters. This is not conservatism for its own sake — it prevents three real bug classes:
- Case collisions. Postgres by default treats
Janeandjaneas different values, but users and email clients do not. Normalize to lowercase at generation time and enforce uniqueness on the lowercased value. - Homoglyph attacks. Unicode offers characters that look identical: Cyrillic
а(U+0430) versus Latina. If you accept any unicode in identifiers, a scammer can register a username visually identical toadmin. If you really want unicode support, run the input through a unicode normalizer first — but for generated identifiers, just stay ASCII. - URL pain. Dots, spaces, and uppercase letters in usernames break case-sensitive routing, OAuth provider comparisons, and DNS-adjacent assumptions. Slack learned to ban uppercase in handles early for exactly this reason.
When you validate user-chosen handles, apply the same charset rules and run candidate strings through a checker — a quick pass against a username generator shows what sane output looks like before you write your validator.
Rule 2: Never trust the base word
Most generators derive a base from the user's name or email: jane.doe@gmail.com becomes jane.doe then jane.doe2847 on collision. Three things go wrong here:
- The local part is not a name. Emails like
kjsdhf8837@gmail.comorbmxrider69@...produce garbage or profane handles. Filter the local part through a character whitelist and length cap; if too little survives, fall back to generation from scratch. - Reserved words. The username
settingscollides with/settingson subdomain or path-based profile URLs. Maintain a blocklist: system routes (admin,api,login,signup,settings,help,static,assets), your brand terms, and anything your legal team flags. Check the blocklist case-insensitively and after your normalization step. - PII leaks. For some audiences a full-name-based handle is a privacy problem — children's products, anonymous forums, whistleblowing tools. Make generation anonymous by default in those products:
quiet-river-4821instead ofjanedoe2847.
Rule 3: Design collision handling before you need it
Uniqueness checks are always racy. Two requests in the same millisecond can both pass an exists() test and insert the same handle. The fixes, in order of preference:
- A unique constraint on the lowercased username column, with a retry loop around
IntegrityError. The constraint is the source of truth; the pre-check is only an optimization. - Bounded retry: append a short numeric or alphanumeric suffix and retry up to N times. Two or three suffix attempts plus a longer random fallback (
jane-8fk2q) is enough in practice. - Reserve on session start: some products reserve the handle at the first step of signup rather than at account creation, then release it if the flow is abandoned. If you do this, give reservations a TTL — orphaned reservations become collision soup for everyone else.
Whatever loop you build, log the final assignment. "User 123 ended up with jane-8fk2q after 3 retries" is reproducible in tests; "it sometimes picked duplicate names" is not.
Rule 4: Generated does not mean unreadable
A username like usr_7f3a9c2e8b is unique but useless in a support call. Prefer pronounceable patterns: word-word-number (amber-otter-42) or word-word (foggy-meadow plus a check digit on collision). Users remember these, and support can hear them over the phone. Keep the wordlist curated — 30 to 100 common nouns and adjectives, screened for offensive combinations. With 80 adjectives and 80 nouns you have 6,400 clean pairs; adding a two-digit suffix gets you 640,000, plenty for most products.
Also generate and show an alternative on demand. A "shuffle" button next to a prefilled username field removes the most common support question: "can I change this?" Yes they can, and now they do not need to open a ticket to ask.
Rule 5: If the username is also a credential surface, measure it
Some systems let users log in with username + password. In that case the handle becomes part of the attack surface: it must not reveal whether an account exists (return identical errors for "no such user" and "wrong password"), and any auto-issued secrets must follow the same bar as passwords. If your flow creates temporary passwords or invite codes, generate them properly — a password generator with cryptographically random output is the reference point, and you can sanity-check your minimums against a password strength checker before setting policy. The classic failure is issuing 8-character alphanumeric temp passwords with readable entropy and then wondering how accounts get taken over before the user's first login.
A checklist you can ship this week
- Charset
[a-z0-9-_], length 3–30, lowercase enforced at write time. - Uniqueness by database constraint, retry loop on conflict, suffix fallback capped.
- Blocklist of system routes and brand terms, checked after normalization.
- Display name stored separately, editable freely.
- Pronounceable pattern for anonymous users, name-derived base only after filtering.
- Error messages identical whether the handle exists or not.
None of this is exotic. It is a whitelist regex, a unique index, a 20-entry blocklist, and a retry loop — roughly an afternoon of work. The reason signups still break in production is that username handling gets written once, at the start of a project, before anyone knows the product will have profile URLs, support agents, and a database full of real emails. Writing the rules down before the form exists is cheaper than migrating 400,000 users off Jane.Doe!2847 later.
When you are designing the form and validator, generating a batch of sample handles with a username generator takes thirty seconds and immediately shows what your rules produce at scale — including the collisions and the reserved-word hits you would otherwise discover from users.