Я сделал проверку валидации в PHP, и я искал каждое поле, чтобы узнать, работает ли код в соответствии с партией. Когда я нажимаю кнопку отправки, единственной частью, не работающей, является код пароля / подтверждения пароля. Я занимаюсь поиском неисправностей в течение нескольких часов и не могу найти проблему.
Возможно, кто-то может указать на проблему? Благодарю.
<?php // define variables and set to empty values $emailErr = $userErr = $passwordErr = $cpasswordErr = $firstErr = $lastErr = $teamErr = ""; $email = $username = $password = $cpassword = $firstname = $lastname = $teamname = ""; // The preg_match() function searches a string for pattern, returning true if the pattern exists, and false otherwise. if ($_SERVER["REQUEST_METHOD"] == "POST") { //Validates email if (empty($_POST["email"])) { $emailErr = "You Forgot to Enter Your Email!"; } else { $email = test_input($_POST["email"]); // check if e-mail address syntax is valid if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "You Entered An Invalid Email Format"; } } //Validates Username if (empty($_POST["username"])) { $userErr = "You Forgot to Enter Your Username!"; } else { $username = test_input($_POST["username"]); } //Validates password & confirm passwords. if(!empty($_POST["password"]) && ($_POST["password"] == $_POST["cpassword"])) { $password = test_input($_POST["password"]); $cpassword = test_input($_POST["cpassword"]); if (strlen($_POST["password"]) <= '8') { $passwordErr = "Your Password Must Contain At Least 8 Characters!"; } elseif(!preg_match("#[0-9]+#",$password)) { $passwordErr = "Your Password Must Contain At Least 1 Number!"; } elseif(!preg_match("#[AZ]+#",$password)) { $passwordErr = "Your Password Must Contain At Least 1 Capital Letter!"; } elseif(!preg_match("#[az]+#",$password)) { $passwordErr = "Your Password Must Contain At Least 1 Lowercase Letter!"; } else { $cpasswordErr = "Please Check You've Entered Or Confirmed Your Password!"; } } //Validates firstname if (empty($_POST["firstname"])) { $firstErr = "You Forgot to Enter Your First Name!"; } else { $firstname = test_input($_POST["firstname"]); //Checks if name only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$firstname)) { $firstErr = "Only letters and white space allowed"; } } if (empty($_POST["lastname"])) { $lastErr = "You Forgot to Enter Your Last Name!"; } else { $lastname = test_input($_POST["lastname"]); //Checks if name only contains letters and whitespace if (!preg_match("/^[a-zA-Z ]*$/",$lastname)) { $lastErr = "Only letters and white space allowed"; } } if (empty($_POST["teamname"])) { $teamErr = "You Forgot to Enter Your Team Name!"; } else { $teamname = test_input($_POST["teamname"]); } } /*Each $_POST variable with be checked by the function*/ function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>
if(!empty($_POST["password"]) && ($_POST["password"] == $_POST["cpassword"])) { $password = test_input($_POST["password"]); $cpassword = test_input($_POST["cpassword"]); if (strlen($_POST["password"]) <= '8') { $passwordErr = "Your Password Must Contain At Least 8 Characters!"; } elseif(!preg_match("#[0-9]+#",$password)) { $passwordErr = "Your Password Must Contain At Least 1 Number!"; } elseif(!preg_match("#[AZ]+#",$password)) { $passwordErr = "Your Password Must Contain At Least 1 Capital Letter!"; } elseif(!preg_match("#[az]+#",$password)) { $passwordErr = "Your Password Must Contain At Least 1 Lowercase Letter!"; } else { $cpasswordErr = "Please Check You've Entered Or Confirmed Your Password!"; } }
Должно быть:
if(!empty($_POST["password"]) && ($_POST["password"] == $_POST["cpassword"])) { $password = test_input($_POST["password"]); $cpassword = test_input($_POST["cpassword"]); if (strlen($_POST["password"]) <= '8') { $passwordErr = "Your Password Must Contain At Least 8 Characters!"; } elseif(!preg_match("#[0-9]+#",$password)) { $passwordErr = "Your Password Must Contain At Least 1 Number!"; } elseif(!preg_match("#[AZ]+#",$password)) { $passwordErr = "Your Password Must Contain At Least 1 Capital Letter!"; } elseif(!preg_match("#[az]+#",$password)) { $passwordErr = "Your Password Must Contain At Least 1 Lowercase Letter!"; } } elseif(!empty($_POST["password"])) { $cpasswordErr = "Please Check You've Entered Or Confirmed Your Password!"; } else { $passwordErr = "Please enter password "; }
Ваша проверка несовпадающих паролей находилась в пределах, если они были проверены, чтобы убедиться, что они совпали.