I am not clear with regular expression of php. I would like to validate password with below rules:
- Password contain at least 8 characters
- Password contain at least 1 letter
- Password contain at least 1 digit
Could anyone help me?
I am not clear with regular expression of php. I would like to validate password with below rules:
Could anyone help me?
public function checkPassword($pwd, &$errors) {
$errors_init = $errors;
if (strlen($pwd) < 8) {
$errors[] = "Password too short!";
}
if (!preg_match("#[0-9]+#", $pwd)) {
$errors[] = "Password must include at least one number!";
}
if (!preg_match("#[a-zA-Z]+#", $pwd)) {
$errors[] = "Password must include at least one letter!";
}
return ($errors == $errors_init);
}
You could use this regex :
preg_match('/(^(?=.*\d)(?=.*[a-z]).{8,}$)/i', $password);
Details :
(?=.*\d)
: at least one digit(?=.*[a-z])
: at least one letter.{8,}
: 8 or more characters/i
: case insensitive regex