User Name Match Regular Expression broken?

User

Well-known member
I have read the following threads:
[Suggestion] Disable/Enable Special Characters in Username
User Name Match Regular Expression
Disallow username spaces
Regex for usernames in registeration form.

As far as I can tell none of the suggestions worked, here is what the most promising one looks like:
regex.png


nomatch.png


The result is the same whether the regex has a preceeding / or not.
Is it busted?

All I want to do is only allow user names that have letters and/or numbers and no more than a single space in them.
 
You just write the regex part itself, not the delimiters. So you'd just do:

^[a-z0-9\s]+$
Thanks. Is the \s an issue since it would match not just space but also whitespace like a tab?
It also seems like XF "autocorrects" multiple consecutive spaces into a single space?

Code:
Us    er becomes Us er
This is desired, just confirming that XF actually does that automagically.
 
It also seems like XF "autocorrects" multiple consecutive spaces into a single space?
Correct. You could just use a literal space as well. Technically speaking, that regex doesn't allow just one space in the name (in total), if that's what you were specifically after.

For one space in total, I think you'd have to use this (untested):

^[a-z0-9]+( [a-z0-9]+)?$
 
This seems to work, though perhaps unnecessarily strict since it will match single character user names which most board probably won't allow:

Code:
^[a-z0-9]++(?:\s[a-z0-9]++)?$

What this does is only allow latin letters, numbers, and a single space in the user name. Meaning that users can't have a phase as their user name.

"My Username" will work.
"My awesome Username" will not work since more than one space in name.
 
I was able to register that name using that RegEx on my test board. Are you sure you don't have any other characters within your validation field?
 
^[A-Za-z0-9 -_]+$

^ is an anchor and matches the beginning of the string
[] matches a single character contained within the brackets
+ repeats the previous match one or more times
$ is an anchor and matches the end of the string

So the above expression matches any uppercase or lowercase character from a to z, the digits 0 to 9 as well as space, hyphen and underscore, one or more times.

(\s matches any whitespace and is not needed)


My name is Brogan and I know (some) RegEx :D
 
Top Bottom