Advertisement
vvccs

wt_10_form_validation

Oct 13th, 2024 (edited)
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.61 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Form Validation Using Regular Expressions</title>
  8. </head>
  9.  
  10. <body>
  11.     <h2>Registration Form</h2>
  12.     <form method="POST" action="">
  13.         <label for="email">Email:</label><br>
  14.         <input type="email" id="email" name="email"
  15.             value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>" required><br><br>
  16.         <label for="username">Username (5-15 characters, letters, and numbers only):</label><br>
  17.         <input type="text" id="username" name="username"
  18.             value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>"
  19.             required><br><br>
  20.         <label for="password">Password (min 8 characters, at least one letter and one number):</label><br>
  21.         <input type="password" id="password" name="password"
  22.             value="<?php echo isset($_POST['password']) ? htmlspecialchars($_POST['password']) : ''; ?>"
  23.             required><br><br>
  24.         <input type="submit" name="submit" value="Submit">
  25.     </form>
  26.     <?php
  27.     // Run the validation logic only after the form is submitted
  28.     if ($_SERVER["REQUEST_METHOD"] == "POST") {
  29.         // Get user inputs from form submission
  30.         $email = $_POST['email'];
  31.         $username = $_POST['username'];
  32.         $password = $_POST['password'];
  33.         // Function to validate email using regex
  34.         function validateEmail($email)
  35.         {
  36.             return preg_match("/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/", $email);
  37.         }
  38.         // Function to validate username using regex
  39.         function validateUsername($username)
  40.         {
  41.             return preg_match("/^[a-zA-Z0-9]{5,15}$/", $username);
  42.         }
  43.         // Function to validate password using regex
  44.         function validatePassword($password)
  45.         {
  46.             return preg_match("/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/", $password);
  47.         }// Validate the inputs
  48.         $isEmailValid = validateEmail($email);
  49.         $isUsernameValid = validateUsername($username);
  50.         $isPasswordValid = validatePassword($password);
  51.         // Output validation results after form submission
  52.         echo "<h3>Validation Results:</h3>";
  53.         echo "Email validation: " . ($isEmailValid ? "Valid" : "Invalid") . "<br>";
  54.         echo "Username validation: " . ($isUsernameValid ? "Valid" : "Invalid") . "<br>";
  55.         echo "Password validation: " . ($isPasswordValid ? "Valid" : "Invalid") . "<br>";
  56.     }
  57.     ?>
  58. </body>
  59.  
  60. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement