What are the rules for usernames?

(Dan Friedman) #1

Continuing the discussion from Official Single-Sign-On for Discourse:

When registering new users with usernames, it is helpful to know what the rules are, so we can let our users know upfront. Here are what I’ve figured out through trial and error.

Minimum length: 3 characters
Maximum length: 15 characters
Must begin with a letter or number
All other characters can be a letter, number or underscore

Overall regex: ^[^_]\w{2,14}$

Eventually, I’d like to make a web service call to Discourse to determine whether the username is valid. This way, if the rules change, my SSO signup adjusts accordingly.

Usernames with periods are changed to underscore
How to allow two-char usernames?
Discourse gem group_add
(Dan Friedman) #2

This regex seems easier to read and less error prone: ^(?!_)\w{3,15}$

(Jeff Atwood) #3

Same username rules as Twitter.

(Dan Friedman) #4

OK, so I found this to compare to and then went to Twitter to verify the differences.

Twitter’s minimum length: 1 character
Discourse’s minimum length: 3 characters

Twitter’s first character can be a letter, number or underscore
Discourse’s first character can be a letter or number, but not an underscore.

With Twitter, just an underscore is a valid username.

So Discourse’s rules are actually a superset of Twitter rules.

(Sam Saffron) #5

Rules have just been updated per


You can view them in: discourse/username_validator.rb at master · discourse/discourse · GitHub

I described them in:

Note, we still fail the “twitter” test in that _ is not valid as a username nor is bob_ but I would argue that this is just confusing and no point allowing this anyway.

(Dan Dascalescu) #6

Further updates, since inspecting the UsernameValidator class doesn’t inform of the length:

(Robert Lauriston) #7

If bob_ is not a valid user name, it would be nice if the system would reject it.