/    Sign up×
Community /Pin to ProfileBookmark

Anybody know the rule for preg_match(ing) a password that has at least 6 letters with at least 1 being a number?

7hduej = valid
hfnjeu = invalid
4hhdf = invalid
hf7hf8yhj = valid
745843 = valid

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@andre4s_yApr 29.2008 — is this pattern work ?

$pattern = "#(?<=[a-z0-9]{6})(?<![a-z]{6})[a-z0-9]*#";
Copy linkTweet thisAlerts:
@SyCoApr 29.2008 — Bit of a melon twister this one ? BTW andre4s_y I tried that one and it didn't work

I wasn't able to do it on a single expression but really hope someone else will as I'd like to see how it should be done.

Hope this gets you by in the mean time!!

I went for a double preg in an if(). First regexp counts min chars, second checks for at least one number. All examples passed valid.
[code=php]/*
7hduej = valid
hfnjeu = invalid
4hhdf = invalid
hf7hf8yhj = valid
745843 = valid
*/
$pw='745843';
if(preg_match("/[a-z0-9]{6,}/i", $pw) && preg_match("/[0-9]{1,}/i", $pw)){
echo 'true';
}else{
echo 'false';
}
[/code]


As per the OP request all numbers can be considered valid however if you wanted to only have a mix of numbers and letter be considered valid I needed yet another preg. sheesh! There's got to be a better way!!

Here's the required mix of letters and numbers

min 6 chars and at least one letter and one number, case insensitive.

[code=php]if(preg_match("/[a-z0-9]{6,}/i", $pw) && preg_match("/[a-z]{1,}/i", $pw) && preg_match("/[0-9]{1,}/", $pw)){
echo 'true';
}else{
echo 'false';
}[/code]
Copy linkTweet thisAlerts:
@andre4s_yApr 29.2008 — Whoa....:eek: it did not work?? Hm...

this is my code :
[code=php]
<?php
$input[]="7hduej";$correctness[]="valid";
$input[]="hfnjeu";$correctness[]="invalid";
$input[]="4hhdf";$correctness[]="invalid";
$input[]="hf7hf8yhj";$correctness[]="valid";
$input[]="745843";$correctness[]="valid";
$pattern = "#(?<=[a-z0-9]{6})(?<![a-z]{6})[a-z0-9]*#";
for($i=0;$i<count($input);$i++)
{
if(preg_match($pattern,$input[$i]))
$output[$i] = "valid";
else
$output[$i] = "invalid";
}
print_r($input);echo "-> try <br />";
print_r($output);echo "-> answer <br />";
print_r($correctness);echo "-> correct answer <br />";
?>
[/code]

The output is :
Array ( [0] => 7hduej [1] => hfnjeu [2] => 4hhdf [3] => hf7hf8yhj [4] => 745843 ) -> try

Array ( [0] => valid [1] => invalid [2] => invalid [3] => valid [4] => valid ) -> answer

Array ( [0] => valid [1] => invalid [2] => invalid [3] => valid [4] => valid ) -> correct answer[/QUOTE]

My pattern is wrong? or My code is wrong? ?
Copy linkTweet thisAlerts:
@SyCoApr 29.2008 — sorry tried again and it did work. I must have had something cached or something!

Care to talk us through what yours does as a learning exp for me at least ?
Copy linkTweet thisAlerts:
@andre4s_yApr 29.2008 — because i am not 100% sure that the pattern is correct.. ?

i only test it with the inputs provided by MrCoder.

The pattern has the same logic as yours : double preg in an if().

First subpattern is positif lookbehind assertions to make sure that input have minimal 6 letters(minimum chars allowed).

Second subpattern is negatif lookbehind assertions to make sure that there is minimum 1 number.

And the last is to make sure that input can be more that 6 letters..

But : i think there is something miss in my pattern.. i can not figure it out yet..

cheersssss... ?
Copy linkTweet thisAlerts:
@SyCoApr 29.2008 — edit: sorry in an idiot, move along!!
Copy linkTweet thisAlerts:
@andre4s_yApr 29.2008 — Patching... [after watching MU vs Barca]
[code=php]
<?php
$input[]="7hduej";$correctness[]="valid";
$input[]="hfnjeu";$correctness[]="invalid";
$input[]="4hhdf";$correctness[]="invalid";
$input[]="hf7hf8yhj";$correctness[]="valid";
$input[]="745843";$correctness[]="valid";
$input[]="aassdd1";$correctness[]="valid";
$input[]="^aassdd1";$correctness[]="invalid";
for($i=0;$i<count($input);$i++)
{
(strlen($input[$i])<=6) ? $strlen=6 : $strlen=strlen($input[$i]);
$pattern = "#(?<=[a-z0-9]{".$strlen."})(?<![D]{".$strlen."})#";
if(preg_match($pattern,$input[$i]))
$output[$i] = "valid";
else
$output[$i] = "invalid";
}
print_r($input);echo "-&gt; try <br />";
print_r($output);echo "-&gt; answer <br />";
print_r($correctness);echo "-&gt; correct answer <br />";
?>
[/code]

the output is :

Array ( [0] => 7hduej [1] => hfnjeu [2] => 4hhdf [3] => hf7hf8yhj [4] => 745843 [5] => aassdd1 [6] => ^aassdd1 ) -> try

Array ( [0] => valid [1] => invalid [2] => invalid [3] => valid [4] => valid [5] => valid [6] => invalid ) -> answer

Array ( [0] => valid [1] => invalid [2] => invalid [3] => valid [4] => valid [5] => valid [6] => invalid ) -> correct answer
[/quote]

The key is : minimum is 6.. maximum is no limit...
Copy linkTweet thisAlerts:
@MrCoderauthorApr 30.2008 — [code=php]
$pattern = "#(?<=[a-z0-9]{6,})(?<![D]{6,})#";
[/code]


Would this remove the need for the strlen() calls?
Copy linkTweet thisAlerts:
@andre4s_yApr 30.2008 — would not.. because lookbehind assertions must have fixed length..
Copy linkTweet thisAlerts:
@bokehApr 30.2008 — [code=php]
$pattern = "#(?<=[a-z0-9]{6,})(?<![D]{6,})#";
[/code]


Would this remove the need for the strlen() calls?[/QUOTE]
That's not a proper check because it allows illegal chars before and after the section it is checking. Also no need for look behind. Maybe try something like the following, which does check string length.[CODE]^(?=(?![D]+$)[a-z0-9]{6,}$)[/CODE]
Copy linkTweet thisAlerts:
@MrCoderauthorApr 30.2008 — <i>
</i>^(?=(?![D]+$)[a-z0-9]{6,}$)


^ = start of string

[a-z0-9]{6,} = only match 6 or more a-z / 0-9 letters

$ = end of string

[D]+ = 1 or more numbers

I understand the above parts of the regex, could you please explain the other elements?

^[COLOR="Red"](?=(?![/COLOR][D]+$[COLOR="Red"])[/COLOR][a-z0-9]{6,}$[COLOR="Red"])[/COLOR]

Thanks!
Copy linkTweet thisAlerts:
@andre4s_yApr 30.2008 — <i>
</i>^(?=(?![D]+$)[a-z0-9]{6,}$)

Super!!!

<:-P
Copy linkTweet thisAlerts:
@bokehApr 30.2008 — <i>
</i>^[COLOR="Magenta"](?=[/COLOR][COLOR="SeaGreen"](?![D]+[COLOR="Red"]$[/COLOR])[/COLOR][a-z0-9]{6,}[COLOR="Red"]$[/COLOR][COLOR="Magenta"])[/COLOR]
[/QUOTE]




^ = start of string

[a-z0-9]{6,} = only match 6 or more a-z / 0-9 letters

[COLOR="Red"]$ [/COLOR]= end of string

[D]+ = 1 or more numbers

[COLOR="Magenta"](?=[/COLOR] = start of positive look ahead

[COLOR="Magenta"])[/COLOR] = end of positive look ahead

[COLOR="SeaGreen"](?![D]+[COLOR="Red"]$[/COLOR])[/COLOR] = Negative look ahead.

It's two zero width assertions. They could be written separately but I chose to nest them. I do this to avoid confusion reading the expression, just my preference.
Copy linkTweet thisAlerts:
@MrCoderauthorMay 01.2008 — Thanks guys, boken.

Works like a charm, I need to read this book on regex that has been sat on my desk for a month now lol.

"Mastering Regular Expressions" - O'Reilly.

?
Copy linkTweet thisAlerts:
@bokehMay 01.2008 — [D]+ = 1 or more numbers[/QUOTE]By the way it's the opposite, i.e. anything that is not a decimal digit. d is a decimal digit and D is anything that is not.
×

Success!

Help @MrCoder spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...