Page 1 of 1

PHP preg_match() username validation question

Posted: Wed Oct 13, 2010 10:07 am
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)

Re: PHP preg_match() username validation question

Posted: Wed Oct 13, 2010 1:27 pm
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!
:)

Re: PHP preg_match() username validation question

Posted: Wed Oct 13, 2010 3:46 pm
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.

Re: PHP preg_match() username validation question

Posted: Wed Oct 13, 2010 3:52 pm
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". :)