First of all, if you have to use regular expressions then your strlen check isn’t going to be accepted.
And please take note or comment on how silly this is. I’m hoping this is only to learn regex, because when you have perfectly good functions to run instead, regex is just a slow and unreadable way of doing things.
Also the password policy is stupid as hell, please refer to: http://xkcd.com/936/
Consider this code:
[php]<?php
$passwords = array(
‘pass’,
‘12?b5A’,
‘A234567?’,
‘aBcdefg?’,
‘Mno edf1?’,
‘abcde1?n’,
‘Sasuke!Naruto9’,
‘Anime_Rules!’,
‘Fairy_Tail2’,
‘Natsu_dragoneal1’
);
$result = array();
foreach ($passwords as $password) {
$result[$password] = array(
‘status’ => ‘’,
‘errors’ => array()
);
if (!preg_match('#(.){7,2048}#',$password)) {
$result[$password]['errors'][] = "Password too short!";
}
if (preg_match('#\s#', $password)) {
$result[$password]['errors'][] = "Password cannot contain spaces!";
}
if (!preg_match('#\d#',$password)) {
$result[$password]['errors'][] = "Password must include at least one number!";
}
if (!preg_match('#[a-z]+#',$password)) {
$result[$password]['errors'][] = "Password must include at least one lower case letter!";
}
if (!preg_match('#[A-Z]+#',$password)) {
$result[$password]['errors'][] = "Password must include at least one upper case letter!";
}
if (!preg_match('#(\W|_)#',$password)) {
$result[$password]['errors'][] = "Password must include at least one symbol!";
}
$result[$password]['status'] = empty($result[$password]['errors']) ? 'Valid' : 'Invalid';
}
?>
Password Strength
<?php foreach ($result as $password => $data) { ?>
<?= $password ?>
Status: <?= $data['status'] ?>
<?php if ($data['status'] === 'Invalid') { ?>
Errors:
<?php foreach ($data['errors'] as $error) { ?>
- <?= $error ?>
<?php } ?>
<?php } ?>
<?php } ?>
[/php]
The result array looks like this:
[code]Array
(
[pass] => Array
(
[status] => Invalid
[errors] => Array
(
[0] => Password too short!
[1] => Password must include at least one number!
[2] => Password must include at least one upper case letter!
[3] => Password must include at least one symbol!
)
)
[12?b5A] => Array
(
[status] => Invalid
[errors] => Array
(
[0] => Password too short!
)
)
[A234567?] => Array
(
[status] => Invalid
[errors] => Array
(
[0] => Password must include at least one lower case letter!
)
)
[aBcdefg?] => Array
(
[status] => Invalid
[errors] => Array
(
[0] => Password must include at least one number!
)
)
[Mno edf1?] => Array
(
[status] => Invalid
[errors] => Array
(
[0] => Password cannot contain spaces!
)
)
[abcde1?n] => Array
(
[status] => Invalid
[errors] => Array
(
[0] => Password must include at least one upper case letter!
)
)
[Sasuke!Naruto9] => Array
(
[status] => Valid
[errors] => Array
(
)
)
[Anime_Rules!] => Array
(
[status] => Invalid
[errors] => Array
(
[0] => Password must include at least one number!
)
)
[Fairy_Tail2] => Array
(
[status] => Valid
[errors] => Array
(
)
)
[Natsu_dragoneal1] => Array
(
[status] => Valid
[errors] => Array
(
)
)
)[/code]
Output:
[code]pass
Status: Invalid
Errors:
Password too short!
Password must include at least one number!
Password must include at least one upper case letter!
Password must include at least one symbol!
12?b5A
Status: Invalid
Errors:
Password too short!
A234567?
Status: Invalid
Errors:
Password must include at least one lower case letter!
aBcdefg?
Status: Invalid
Errors:
Password must include at least one number!
Mno edf1?
Status: Invalid
Errors:
Password cannot contain spaces!
abcde1?n
Status: Invalid
Errors:
Password must include at least one upper case letter!
Sasuke!Naruto9
Status: Valid
Anime_Rules!
Status: Invalid
Errors:
Password must include at least one number!
Fairy_Tail2
Status: Valid
Natsu_dragoneal1
Status: Valid[/code]