我正在尝试检查密码以确保它们至少包含1个大写,小写和一个数字。我已经调查了这个,但是每次都找不到原因,它返回false。例如,我放入了Thechedda123,它仍然返回false,并且不会发布到我的数据库。
$password = $_GET["password"];
$regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$";
$query="INSERT INTO `users`(`username`, `password`, `school`, `grade`,`email`, `classes`, `firstname`, `lastname`) VALUES ('$username','$password','$school','$grade','$email','$school','$firstname','$firstname')";
if (preg_match($regex, $password)){
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message = 'Whole query: ' . $query;
die($message);
}
}else{
$numb = 'true';
$cap = 'true';
if (preg_match($regex, $password)){
$numb = 'false';
}
if (preg_match($regex, $password)){
$cap = 'false';
}
echo $cap;
echo $numb;
}
?>
答案 0 :(得分:0)
我编辑并测试了你的正则表达式。将正则表达式替换为:
$regex = "/^((?=.*[a-z])(?=.*[A-Z])(?=.*\d))[a-zA-Z\d]{8,}$/";
并测试:
$password_good = "Thecedda123"; // Ok
$password_bad_1 = "hechedda123"; // Upper case missed
$password_bad_2 = "TTTTTTTTTT123"; // Lower case missed
$password_bad_3 = "Thechedda"; // Digit missed
$password_bad_4 = "Th123"; // Less then 8 characters
$password_bad_5 = "1234567890"; // Letters missed
// Result:
echo preg_match($regex, $password_good); // true
echo preg_match($regex, $password_bad_1); // false
echo preg_match($regex, $password_bad_2); // false
echo preg_match($regex, $password_bad_3); // false
echo preg_match($regex, $password_bad_4); // false
echo preg_match($regex, $password_bad_5); // false