Page 1 of 1

Password validation with regex

Posted: Thu Mar 15, 2007 1:41 am
by kwong
Hi All,
I'm not a new member actually, join this forum last year but being passive all time, for some reason I can't login even though I have requested a new password.
Okay. get to the point.

this is my pattern :

Code: Select all

$pattern = "/^[1-5]([1-5[:alpha:]\*\$){4,14}[1-5]$/";
I need to check a password with this condition:
1. First and last character must be integer
2. Password must be between 6 and 16 characters long.
3. Acceptable character are alpha characters and number 1 - 5
4. Must at least contains one instance of * and $

With the pattern above I manage to get 1st, 2nd, 3rd condition, but fail in 4th condition.
The preg-match() will return true though no * or $ is entered.

Can anybody help with this?

Thanks a lot

Cheers
Kal

Posted: Thu Mar 15, 2007 2:07 am
by Kieran Huggins
so close!

Code: Select all

#^[1-5][a-z1-5\*\$]{4,14}[1-5]$#
You may find this helpful:
http://www.cuneytyilmaz.com/prog/jrx/

Posted: Thu Mar 15, 2007 6:26 am
by kwong
Thank you Kieran, however when I apply your pattern and test it with 111111, it returns true, it's supposed to return false.

Posted: Thu Mar 15, 2007 8:01 am
by feyd
Can you post several test values and their expected pass/fail status?

Posted: Thu Mar 15, 2007 8:46 am
by kwong
The condition is min 6 characters and maximum 16, first and last character must be numbers between 1-5, must contains at least one * and one $, permittable characters: alpha and number 1-5.

Feyd, here are pass and fail example(min 6 characters):

"111111" should fail-> no * and $
"123456" should fail -> do not accept 6
"1a*s$1" is pass -> 6 characters long, first and last characters is number between 1 - 5, contains * and $ (once)

Thanks for any answers

Posted: Thu Mar 15, 2007 9:29 am
by stereofrog
Something like this maybe

Code: Select all

$re = '/
	^
	[1-5]
	(?= .{4,14} [1-5] $)
	(?=
		[a-z1-5]*\*
		[a-z1-5]*\$
		[a-z1-5*$]*
		|
		[a-z1-5]*\$
		[a-z1-5]*\*
		[a-z1-5*$]*
	)
/xD';

Posted: Thu Mar 15, 2007 9:45 am
by kwong
Thanks stereofrog,

Your pattern matches my third example, however it didn't match 1**$$1 (this one should pass, but using your pattern it fails).

anyway I notice the ?= in your pattern and look like you define what it is later in a separate sub condition.

I have an idea, I will try it and see if it works

Thanks again everyone

Posted: Thu Mar 15, 2007 10:43 am
by Kieran Huggins
maybe 2 regexes? (I know, it's sloppy)

Code: Select all

if( preg_match('#^[1-5][a-z1-5\*\$]{4,14}[1-5]$#',$password) == 1 && preg_match('#[^\*\$]#',$password) == 0)

Posted: Thu Mar 15, 2007 5:46 pm
by kwong
Thanks everyone,

I've got no time, I submitted my work last nite, I ended up with 3 regex.
Here how I did it

Code: Select all

$pattern = "/^[1-5][(1-5[]*$)]{4,14}[1-5]$/";
$pattern2 = "/[*]+/";
$pattern3 = "/[$]+/";

if(preg_match($pattern, $message)){
   if(preg_match_all($pattern2, $message, $match1)){
      if(preg_match_all($pattern3, $message, $match2)) echo "Match"; else echo "Not Match Pattern 3";

  } else echo "Not Match Pattern 2";
} else echo "Not Match Pattern 1";
I'd appreciate if anyone can join the three pattern.

Cheers
Kal

Posted: Fri Mar 16, 2007 3:35 pm
by Ollie Saunders
One regex plus 2 strposes for the "must have" '*' and '$'
I'd say.