Results 1 to 5 of 5

Thread: [RESOLVED] Regular expressions - Username

  1. #1

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Resolved [RESOLVED] Regular expressions - Username

    I already have a function to check for a valid e-mail address using regular expressions, but I had written it awhile ago when I was still familiar with them.

    Now I need one to check for a valid username. The username can only have letters, numbers, underscores (_), spaces, and a single period.

    It has to start with a letter. It cannot have consecutive spaces (2 or more in a row).

    Can someone help me?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Regular expressions - Username

    Something like

    [a-zA-Z]([a-zA-Z0-9_]|( [a-zA-Z0-9_]))*(\.([a-zA-Z0-9_]|( [a-zA-Z0-9_]))*)?

    In words: with a name character being an alphanumeric character or an underscore, a username is
    an alphabetic character followed by
    either a name character, or a space and a name character, repeated any number of times
    followed optionally by
    a dot and
    either a name character, or a space and a name character, repeated any number of times

    If you don't want to allow the dot as the last character, replace the last * by a +.
    If you don't want to allow the dot as the last character and don't want a space to follow the dot, insert a name character collection after the dot, unconditionally.
    As it is, the space character cannot be the last character, nor can it be directly before the dot.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Regular expressions - Username

    I hope you realise that properly validating email addresses using regular expressions is fairly abstruse. Simple validation is fine, but you invariably cut out 1% of potential addresses.

  4. #4

    Thread Starter
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Regular expressions - Username

    Quote Originally Posted by CornedBee
    Something like

    [a-zA-Z]([a-zA-Z0-9_]|( [a-zA-Z0-9_]))*(\.([a-zA-Z0-9_]|( [a-zA-Z0-9_]))*)?

    In words: with a name character being an alphanumeric character or an underscore, a username is
    an alphabetic character followed by
    either a name character, or a space and a name character, repeated any number of times
    followed optionally by
    a dot and
    either a name character, or a space and a name character, repeated any number of times

    If you don't want to allow the dot as the last character, replace the last * by a +.
    If you don't want to allow the dot as the last character and don't want a space to follow the dot, insert a name character collection after the dot, unconditionally.
    As it is, the space character cannot be the last character, nor can it be directly before the dot.
    Wow thanks, that looks promising. I'll give it a try and report back.

    Quote Originally Posted by penagate
    I hope you realise that properly validating email addresses using regular expressions is fairly abstruse. Simple validation is fine, but you invariably cut out 1% of potential addresses.
    Wow. I'm not sure how well that would perform in PHP. Especially under high traffic. Shouldn't be too much of a problem, though. If they really want to register they'll use a different e-mail address. Thanks.

  5. #5
    New Member
    Join Date
    Mar 2007
    Posts
    1

    Re: [RESOLVED] Regular expressions - Username

    cornedbee: wow this is good. can you pls modify it to not include white space characters? thanks, greatly appreciate it

    or is this correct
    [a-zA-Z]([a-zA-Z0-9_]|([a-zA-Z0-9_]))*(\\.([a-zA-Z0-9_]|([a-zA-Z0-9_]))*)?
    Last edited by chitgoks; Mar 23rd, 2007 at 02:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width