1. 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?

Discussion in 'Troubleshooting and Problems' started by User, May 27, 2011.

  1. User

    User Well-Known Member

  2. Mike

    Mike XenForo Developer Staff Member

    You just write the regex part itself, not the delimiters. So you'd just do:

  3. User

    User Well-Known Member

    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?

    Us    er becomes Us er
    This is desired, just confirming that XF actually does that automagically.
  4. Mike

    Mike XenForo Developer Staff Member

    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]+)?$
  5. User

    User Well-Known Member

    This seems to work, though perhaps unnecessarily strict since it will match single character user names which most board probably won't allow:

    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.
  6. Stuart Wright

    Stuart Wright Well-Known Member

    I used that. I assume it allows alphanumeric plus spaces. It blocked
  7. Jeremy

    Jeremy Well-Known Member

    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?
  8. Stuart Wright

    Stuart Wright Well-Known Member

    Doh there was a space on the end. Thanks Jeremy :)
  9. leemy

    leemy Member

    Sorry I have no experience with regex.

    I want to allow alphanumeric (uppercase, lowercase, numbers), additionally space, hyphen, and underscore.

    is this it? ^[a-z0-9\s]+$

    Do I need the \s? What is +$?

    This page has a-z and A-Z
    ^[a-zA-Z0-9 ]+$
    Actually @Mike is on both threads I'd appreciate your input!
  10. Brogan

    Brogan XenForo Moderator Staff Member

    ^[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 likes this.
  11. Jeremy

    Jeremy Well-Known Member

    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.
    Brogan likes this.

Share This Page