PHP preg_match() username validation question

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
calimero_php
Forum Newbie
Posts: 4
Joined: Wed Oct 13, 2010 9:42 am

PHP preg_match() username validation question

Post by calimero_php »

Hey,

after quite a bit of time hitting the wall about this pattern, i had to resort to help:

Problem:

Validate username in the following format:
1) It must start with a letter ^[a-z]{1}
2) It can contain any letter or number - amount from 0 to unlimited
3) It can contain from 0 to 1 dot's "." -> \. ( there is addition to this condition explained below )
4) It must end with a letter or number [a-z|0-9]${1}

Now tricky part - total length of the username must be between 3 and 50 => i tried (entire_condition){3,50}


After juggling and trying loads of options - my last pattern looks like this (and yeah its not doing what i am trying to make it do):

Code: Select all

$inp_vr = ( preg_match('/^[a-z]{1}\.?[a-z|0-9]+[a-z|0-9]${1}/', strtolower( trim($inp_vr) ) ) ) ? $inp_vr : '';
The addition to condition 3 - is it in anyway possible to use preg_match() to check that if user inputs dots - they can be used multiple times but only if they are separated by at least 1 letter/number between each dot (aka no chained dots)

Username ex:
a.hoa2a12.3s.5.d1eod - VALID (each dot separated by some letter/number)
a..a23a23a23a23a23a - INVALID (chained dots)
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: PHP preg_match() username validation question

Post by ridgerunner »

To specify a specific length range, lookahead is your friend. Try this out:

Code: Select all

if (preg_match(
    '/# match username from 3-50 chars in length
    ^               # anchor to string start
    (?=.{3})        # minimum length is 3
    (?!.{51})       # maximum length is 50
    [A-Za-z]        # must start with a letter
    [A-Za-z0-9]*    # zero or more alphanum
    (?:             # non-capture group for special dots
      \.            # allow a dot, but only if followed by
      [A-Za-z0-9]+  # at least one alphanum
    )*              # zero or or more dot+alphanum
    $               # anchor to string end
    /x', 
    $contents)) {
    # Successful match
} else {
    # Match attempt failed
}
Hope this helps!
:)
calimero_php
Forum Newbie
Posts: 4
Joined: Wed Oct 13, 2010 9:42 am

Re: PHP preg_match() username validation question

Post by calimero_php »

If i had your brain hours ago (and understanding of the logic behind reg_exp in php).

Yup tested on all versions of input and getting expected output, thank you very much for the code.

If this forum had rating system:
Solution (aplicability) = 10/10
Explanation = 10/10

:D
Now i can work on next module.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP preg_match() username validation question

Post by AbraCadaver »

calimero_php wrote:If i had your brain hours ago (and understanding of the logic behind reg_exp in php).

Yup tested on all versions of input and getting expected output, thank you very much for the code.

If this forum had rating system:
Solution (aplicability) = 10/10
Explanation = 10/10

:D
Now i can work on next module.
Yep, I looked at this hours ago and spent 15 minutes racking my brain and said, "ahh forget it, ridgerunner will be along shortly". :)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply