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

Username validation regex

Discussion in 'Troubleshooting and Problems' started by Mancunian, Dec 4, 2011.

  1. Mancunian

    Mancunian Member

    I am looking for some help on fixing my "Username match Regular Expression".
    I am currently using this:
    ^[a-zA-Z0-9]+$
    This does not permit a space within the username.

    However, I want to allow a single space within the username but not allow multiple spaces.
    So
    John Doe - should be valid - as should John F Doe - i.e. a space between each word.
    but
    John.......Doe is invalid as it has multiple intervening spaces - shown here as dots as I cannot enter white space.

    Can anybody help a I am suffering from a brain freeze with regex.
     
  2. xfrocks

    xfrocks Well-Known Member

    Err, probably this?

    Code:
    ^(\w+ )*\w+$
    PS: change \w to your [...], I'm lazy :p
     
  3. Mancunian

    Mancunian Member

    My problem is that I only want to allow a single space between words that comprise the username..
     
  4. xfrocks

    xfrocks Well-Known Member

    That regex will match
    • John Doe
    • John F Doe
    but not
    • John.......Doe
     
  5. Mancunian

    Mancunian Member

    ^(\w+ )*\w+$
    It did allow me to create a name of John...D.........Doe i.e. with multiple spaces..
     
  6. Mike

    Mike XenForo Developer Staff Member

    Multiple spaces aren't allowed in user names anyway. They get stripped back to 1.
     
    MichaelDance and Mancunian like this.
  7. Mancunian

    Mancunian Member

    Oh bugger! I was only testing the user entry part where he defines his desired userid and at that point it was being accepted... i.e. when you tab to the next field - it wasn't being rejected or stripped of white space at that point.
    OK so in which case my original regex wih a slight mod will be fine with a space after the 9 as follows (as I don't want to allow the underscore which \w permits): ^[a-zA-Z0-9 ]+$

    Thanks Mike.
     
  8. 3c

    3c Active Member

    I must be missing something. I had a user that tried to sign up using the same name she uses on facebook but it wouldn't let her. There is no way it was already taken. Evidently it wouldn't let her use a space.

    Is the string that is already in the matched expression not suitable for allowing spaces?

    What exactly needs to be in that field to allow spaces?
     
  9. Brogan

    Brogan XenForo Moderator Staff Member

    Spaces are allowed by default.
    What do you have in the field?

    What was the error the user received?
     
  10. 3c

    3c Active Member

    ^[a-zA-Z0-9]+$
     
  11. Brogan

    Brogan XenForo Moderator Staff Member

    That doesn't permit spaces to be used.
    Unless you want to specifically block some characters, just clear the regex field and leave it blank.
     
  12. 3c

    3c Active Member

    is there any particular reason for disallowing any characters??
     
  13. Brogan

    Brogan XenForo Moderator Staff Member

    It's personal preference.
    Some like to limit it to alphanumeric characters only.
     
  14. 3c

    3c Active Member

    is there a way to keep it to alpha-numeric and still leave a space?
     
  15. Mike

    Mike XenForo Developer Staff Member

    ^[a-zA-Z0-9 ]+$

    (This is different to what you originally had.)
     
    MichaelDance and 8thos like this.
  16. Brogan

    Brogan XenForo Moderator Staff Member

    Try ^[a-zA-Z0-9 ]+$

    Edit: Beaten by the Regex ninja!
     
    The8thLegion likes this.
  17. 3c

    3c Active Member

    I must need more coffee...took me a minute to see the difference! LOL....

    thanks much Mike!

    wow Brogan....I haven't seen you get ninja'd very often around here!
     
  18. Brogan

    Brogan XenForo Moderator Staff Member

    I think I actually posted first but Mike edited the database :whistle:
     
    Moddis likes this.
  19. 3c

    3c Active Member

    ah..yea, that must have been it! ;)
     
  20. I want to allow everything that Xenforo currently allows in username (spaces, special characters, etc.), but I want to exclude any foreign language characters other than Western European stuff like Á, È, Ö, etc. I also want to exclude any weird special characters (stuff that doesn't show up on a normal US English qwerty keyboard) so someone's username doesn't end up being something like this: ┻ (ノಠ益ಠ)ノ彡┻

    I found this regex string online, but I don't think it's exactly what I need:
    Code:
    ^[\-\.a-zA-Z0-9'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ\\ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ]*$
    Any ideas?
     

Share This Page