//TODO 1: implement JavaScript validation //ensure it returns false for an error and true for success
Posted: Tue Jul 05, 2022 10:33 am
//TODO 1: implement JavaScript validation
//ensure it returns false for anerror and true for success
1 <?php 2 require(_DIR___ . "/../../partials/nav.php"); 3 reset_session(); 4 5 <form onsubmit="return validate(this)" method="POST"> 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 <div> </div> <div> <label for="email">Email</label> <input type="email" name="email" required /> </div> <div> </form> <script> <label for="username">Username</label> <input type="text" name="username" required maxlength="30" /> </div> <div> <label for="pw">Password</label> <input type="password" id="pw" name="password" required minlength="8" /> </div> <input type="submit" value="Register" /> <label for="confirm">Confirm</label> <input type="password" name="confirm" required minlength="8" /> function validate(form) { //TODO 1: implement JavaScript validation //ensure it returns false for an error and true for success } } </script> return true; <?php //TODO 2: add PHP Code if (isset($_POST["email"]) && isset($_POST["password"]) && isset($_POST["confirm"]) && isset($_POST["username"])) { $email = se($_POST, "email", "", false); $password = se($_POST, "password", "", false); $confirm = se($_POST, "confirm", "", false); $username = se($_POST, "username", "", false); //TODO 3 $hasError = false; if (empty($email)) { flash ("Email must not be empty", "danger"); $hasError = true; //sanitize $email = sanitize_email($email); //validate if (lis_valid_email($email)) { flash ("Invalid email address", "danger"); $hasError = true; } } if (!is_valid_username ($username)) { flash ("Username must only contain 3-16 characters a-z, 0-9, or -", "danger"); $hasError = true; } if (empty($password)) { flash ("password must not be empty", "danger"); $hasError = true; } if (empty($confirm)) { flash ("Confirm password must not be empty", "danger"); $hasError = true; } if (lis_valid_password($password)) { flash ("Password too short", "danger"); $hasError = true;
68 69 70 71 73 74 77 78 79 80 81 82 83 84 85 86 } ?> if ( ?> strlen($password) > 0 && $password !== $confirm ) { flash ("Passwords must match", "danger"); $hasError = true; } if (!$hasError) { TODO 4 } $hash= password_hash ($password, PASSWORD_BCRYPT); $db = getDB(); $stmt = $db->prepare("INSERT INTO Users (email, password, username) VALUES(:email, :password, :username)"); try { $stmt->execute([":email" => $email, ":password" => $hash, ":username" => $username]); flash ("Successfully registered!", "success"); } catch (Exception $e) { } 87 88 <?php 89 require(_DIR_ . "/../../partials/flash.php"); 90 You, yesterday housekeeping users_check_duplicate($e->errorInfo);
//ensure it returns false for anerror and true for success
1 <?php 2 require(_DIR___ . "/../../partials/nav.php"); 3 reset_session(); 4 5 <form onsubmit="return validate(this)" method="POST"> 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 <div> </div> <div> <label for="email">Email</label> <input type="email" name="email" required /> </div> <div> </form> <script> <label for="username">Username</label> <input type="text" name="username" required maxlength="30" /> </div> <div> <label for="pw">Password</label> <input type="password" id="pw" name="password" required minlength="8" /> </div> <input type="submit" value="Register" /> <label for="confirm">Confirm</label> <input type="password" name="confirm" required minlength="8" /> function validate(form) { //TODO 1: implement JavaScript validation //ensure it returns false for an error and true for success } } </script> return true; <?php //TODO 2: add PHP Code if (isset($_POST["email"]) && isset($_POST["password"]) && isset($_POST["confirm"]) && isset($_POST["username"])) { $email = se($_POST, "email", "", false); $password = se($_POST, "password", "", false); $confirm = se($_POST, "confirm", "", false); $username = se($_POST, "username", "", false); //TODO 3 $hasError = false; if (empty($email)) { flash ("Email must not be empty", "danger"); $hasError = true; //sanitize $email = sanitize_email($email); //validate if (lis_valid_email($email)) { flash ("Invalid email address", "danger"); $hasError = true; } } if (!is_valid_username ($username)) { flash ("Username must only contain 3-16 characters a-z, 0-9, or -", "danger"); $hasError = true; } if (empty($password)) { flash ("password must not be empty", "danger"); $hasError = true; } if (empty($confirm)) { flash ("Confirm password must not be empty", "danger"); $hasError = true; } if (lis_valid_password($password)) { flash ("Password too short", "danger"); $hasError = true;
68 69 70 71 73 74 77 78 79 80 81 82 83 84 85 86 } ?> if ( ?> strlen($password) > 0 && $password !== $confirm ) { flash ("Passwords must match", "danger"); $hasError = true; } if (!$hasError) { TODO 4 } $hash= password_hash ($password, PASSWORD_BCRYPT); $db = getDB(); $stmt = $db->prepare("INSERT INTO Users (email, password, username) VALUES(:email, :password, :username)"); try { $stmt->execute([":email" => $email, ":password" => $hash, ":username" => $username]); flash ("Successfully registered!", "success"); } catch (Exception $e) { } 87 88 <?php 89 require(_DIR_ . "/../../partials/flash.php"); 90 You, yesterday housekeeping users_check_duplicate($e->errorInfo);