Here’s a simplified version of a validation function that I use:
[php]<?php
$errors = [‘validPassword’ => true, ‘matchPassword’ => true]; // Set all errors parameters initially to true:
/*
- Passing password and verify passord to function, plus
- passing errors array by reference.
/
function passwordCheck($password, $verify, &$errors) {
/
*
- Explaining !preg_match_all(’$\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$’, $password)
- $ = beginning of string
- \S* = any set of characters
- (?=\S{8,}) = of at least length 8
- (?=\S*[a-z]) = containing at least one lowercase letter
- (?=\S*[A-Z]) = and at least one one uppercase letter
- (?=\S*[\d]) = and at least one number
- (?=\S*[\W]) = and at least a special character (non-word character)
- $ = end of the string:
-
/
if (!preg_match_all(’$\S(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])(?=\S*[\W])\S*$’, $password)) {
$errors[‘validPassword’] = false; // False means it’s an invalid password:
} else {
$errors[‘validPassword’] = true;
}
if ($password != $verify) {
$errors[‘matchPassword’] = false; // False means passwords don’t match:
} else {
$errors[‘matchPassword’] = true;
}
}
$password = “Icsan2016!”;
$verifyPassword = “Icsan2016!”;
passwordCheck($password, $verifyPassword, $errors);
if ($errors) {
echo “Everything checks out OK!
\n”;
echo “
” . print_r($errors,1) . “
\n”;
} else {
echo “There is an error somewhere!
\n”;
echo “
” . print_r($errors,1) . “
\n”;
}
if ($errors[‘validPassword’]) {
echo “
Password is valid!
\n”;
} else {
echo “
Password is invalid!!!
\n”;
}
if ($errors[‘matchPassword’]) {
echo “
Both passwords match!
\n”;
} else {
echo “
Passwords do not match!!!
\n”;
}
$password = “Icsan2016a”;
$verifyPassword = “Icsan2016!”;
passwordCheck($password, $verifyPassword, $errors);
if ($errors) {
echo “Everything checks out OK!
\n”;
echo “
” . print_r($errors,1) . “
\n”;
} else {
echo “There is an error somewhere!
\n”;
echo “
” . print_r($errors,1) . “
\n”;
}
if ($errors[‘validPassword’]) {
echo “
Password is valid!
\n”;
} else {
echo “
Password is invalid!!!
\n”;
}
if ($errors[‘matchPassword’]) {
echo “
Both passwords match!
\n”;
} else {
echo “
Passwords do not match!!!
\n”;
}
$password = “Icsan2016!”;
$verifyPassword = “Icsan2016!s”;
passwordCheck($password, $verifyPassword, $errors);
if ($errors) {
echo “Everything checks out OK!
\n”;
echo “
” . print_r($errors,1) . “
\n”;
} else {
echo “There is an error somewhere!
\n”;
echo “
” . print_r($errors,1) . “
\n”;
}
if ($errors[‘validPassword’]) {
echo “
Password is valid!
\n”;
} else {
echo “
Password is invalid!!!
\n”;
}
if ($errors[‘matchPassword’]) {
echo “
Both passwords match!
\n”;
} else {
echo “
Passwords do not match!!!
\n”;
}
[/php]
The password requirements can be easily modified. HTH John
P.S. Can anyone guess what the phrase of the password “Icsan2016!” is? (Hint: Look at the time I posted this) 