• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

User Name Match Regular Expression broken?

User

Well-known member
#1

User

Well-known member
#3
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.
 

Mike

XenForo developer
Staff member
#4
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]+)?$
 

User

Well-known member
#5
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.
 

Jeremy

Well-known member
#7
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?
 

Brogan

XenForo moderator
Staff member
#10
^[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
 

Jeremy

Well-known member
#11
Brogan is growing up and learning RegEx. :D

XenForo actually uses the caseless flag, so a-z actually matches the same thing as A-Za-z.