Advertisement
atanasovetr

DateAndTimeValidator

Mar 14th, 2021
903
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.34 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
  5.     <title>Validator</title>
  6.     <style type="text/css">
  7.         body{
  8.             background-color: #FBB917;
  9.         }
  10.     </style>
  11. </head>
  12. <body>
  13.     <div class="container">
  14.         <form action="#" method="POST">
  15.             <div class="row">
  16.                 <div class="offset-md-4 col-md-4">
  17.                     <label for="date" class="form-label">Date</label>
  18.                     <input type="text" name="date" id="date" class="form-control">
  19.                 </div> 
  20.             </div>
  21.  
  22.             <div class="row">
  23.                 <div class="offset-md-4 col-md-4">
  24.                     <label for="time" class="form-label">Time</label>
  25.                     <input type="text" name="time" id="time" class="form-control">
  26.                 </div>
  27.             </div> 
  28.             <div class="row">
  29.                 <div class="offset-md-4 col-md-4">
  30.                     <input type="submit" name="validate"  class="form-control">
  31.                 </div>
  32.             </div> 
  33.             <div class="row">
  34.                 <div class="offset-md-4 col-md-4">
  35.                     <?php
  36.                         if (isset($_POST["validate"])){
  37.                             $date = $_POST["date"];
  38.                             $re = '/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/m';
  39.                             preg_match_all($re, $date, $matches, PREG_SET_ORDER, 0);
  40.                             if (isset($matches[0][0])){
  41.                                 echo "<h2>".$date." е валидна дата</h2>";
  42.                             } else {
  43.                                 echo "<h2>Невалидна дата</h2>";
  44.                             }
  45.                         }
  46.                        ?>
  47.                 </div>
  48.             </div> 
  49.             <div class="row">
  50.                 <div class="offset-md-4 col-md-4">
  51.                     <?php
  52.                         if (isset($_POST["validate"])){
  53.                             $time = $_POST["time"];
  54.                             $regexTime = "/^(([0-1][0-9])|(2[0-3]))(:[0-5][0-9]){2}$/";
  55.                             if (preg_match($regexTime, $time)){
  56.                                 echo "<h2>".$time." е валидно време</h2>";
  57.                             } else {
  58.                                 echo "<h2>Невалидно време</h2>";
  59.                             }
  60.                         }
  61.                        ?>
  62.                 </div>
  63.             </div> 
  64.         </form>
  65.     </div> 
  66. </body>
  67. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement