Menu
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
[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]
[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]
[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]
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]
[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 "-> try <br />";
print_r($output);echo "-> answer <br />";
print_r($correctness);echo "-> correct answer <br />";
?>
[/code]
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]
[code=php]
$pattern = "#(?<=[a-z0-9]{6,})(?<![D]{6,})#";
[/code]
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=php]
$pattern = "#(?<=[a-z0-9]{6,})(?<![D]{6,})#";
[/code]
Would this remove the need for the strlen() calls?[/QUOTE]
[CODE]^(?=(?![D]+$)[a-z0-9]{6,}$)[/CODE]
<i>
</i>^(?=(?![D]+$)[a-z0-9]{6,}$)
<i>
</i>^[COLOR="Magenta"](?=[/COLOR][COLOR="SeaGreen"](?![D]+[COLOR="Red"]$[/COLOR])[/COLOR][a-z0-9]{6,}[COLOR="Red"]$[/COLOR][COLOR="Magenta"])[/COLOR]
[/QUOTE]
0.1.9 — BETA 5.29