Password validation with regex

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

Moderator: General Moderators

Post Reply
kwong
Forum Newbie
Posts: 10
Joined: Thu Mar 15, 2007 1:17 am

Password validation with regex

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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/
kwong
Forum Newbie
Posts: 10
Joined: Thu Mar 15, 2007 1:17 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Can you post several test values and their expected pass/fail status?
kwong
Forum Newbie
Posts: 10
Joined: Thu Mar 15, 2007 1:17 am

Post 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
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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';
kwong
Forum Newbie
Posts: 10
Joined: Thu Mar 15, 2007 1:17 am

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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)
kwong
Forum Newbie
Posts: 10
Joined: Thu Mar 15, 2007 1:17 am

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

One regex plus 2 strposes for the "must have" '*' and '$'
I'd say.
Post Reply