Venciity

PIN VALIDATION

Aug 27th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.96 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>PIN Validation</title>
  6. </head>
  7. <body>
  8.     <form action="" method="get">
  9.         <input type="text" name="name" value="Ana Ivanova"/>
  10.         <input type="text" name="gender" value="female"/>
  11.         <input type="text" name="pin" value="9912164756"/>
  12.         <input type="submit"/>
  13.     </form>
  14.     <?php
  15.         if (isset($_GET['name']) && isset($_GET['gender']) && isset($_GET['pin'])) {
  16.             $name = $_GET['name'];
  17.             $gender = $_GET['gender'];
  18.             $pin = $_GET['pin'];
  19.             $isValidNamePin = true;
  20.  
  21.             $namePattern = "/[A-Z][a-z]+\\s[A-Z][a-z]+/";
  22.             preg_match($namePattern, $name, $nameMatch);
  23.             //var_dump($nameMatch);
  24.             $pinPattern = "/[0-9]{10}/";
  25.             preg_match($pinPattern, $pin , $pinMatch);
  26.             //var_dump($pinMatch);
  27.  
  28.             if (empty($nameMatch) || empty($pinMatch)) {
  29.                 $isValidNamePin = false;
  30.             }
  31.  
  32.             $result = [];
  33.             if (isCorrectDate($pin) && isCorrectGender($gender, $pin) && isCorrectChecksum($pin) && $isValidNamePin) {
  34.                 $result['name'] = $name;
  35.                 $result['gender'] = $gender;
  36.                 $result['pin'] = $pin;
  37.                 echo json_encode($result);
  38.             }
  39.             else {
  40.                 echo "<h2>Incorrect data</h2>";
  41.             }
  42.         }
  43.     function isCorrectDate($pin) {
  44.         $date = substr($pin, 0, 6);
  45.         $year = intval(substr($date, 0, 2));
  46.         $month = intval(substr($date, 2, 2));
  47.         $day = intval(substr($date, 4, 2));
  48.  
  49.         if ($month > 20 && $month <= 32) {
  50.             $month -= 20;
  51.         }else if ($month > 40 && $month <= 52) {
  52.             $month -= 40;
  53.         }
  54.  
  55.         if (checkdate($month, $day , $year)) {
  56.             return true;
  57.         }
  58.         return false;
  59.     }
  60.  
  61.     function isCorrectGender($gender, $pin) {
  62.         $genderDigit = substr($pin, -2, 1);
  63.         if ($genderDigit % 2 == 0 && $gender == "male") { // if genderDigit is even and gender == male
  64.             return true;
  65.         }else if ($genderDigit % 2 == 1 && $gender == "female") { // if genderDigit is odd and gender == female
  66.             return true;
  67.         }
  68.         return false;
  69.     }
  70.  
  71.     function isCorrectChecksum($pin) {
  72.         $checksumDigit = intval(substr($pin, -1));
  73.         $weights = [2, 4, 8, 5, 10, 9, 7, 3, 6];
  74.         $checkSum = 0;
  75.  
  76.         for ($i = 0; $i < 9; $i++) {
  77.             $checkSum += intval(substr($pin, $i, 1)) * $weights[$i];
  78.         }
  79.  
  80.         $remainder = $checkSum % 11;
  81.         if ($remainder == 10) {
  82.             $remainder = 0;
  83.             if ($remainder == $checksumDigit) {
  84.                 return true;
  85.             }
  86.         }
  87.         else {
  88.             if ($remainder == $checksumDigit) {
  89.                 return true;
  90.             }
  91.         }
  92.         return false;
  93.     }
  94.     ?>
  95. </body>
  96. </html>
Advertisement
Add Comment
Please, Sign In to add comment