PHP preg_match() username validation question
Posted: Wed Oct 13, 2010 10:07 am
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):
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)
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 : '';Username ex:
a.hoa2a12.3s.5.d1eod - VALID (each dot separated by some letter/number)
a..a23a23a23a23a23a - INVALID (chained dots)