Postcode validation. 2013-10-03
authorIain Patterson <me@iain.cx>
Thu, 3 Oct 2013 07:51:54 +0000 (03:51 -0400)
committerIain Patterson <me@iain.cx>
Thu, 3 Oct 2013 07:51:54 +0000 (03:51 -0400)
Validate postcode according to the rules described in BS766.

lib/contact.php
lib/functions.php
lib/hub.php

index b680982..051f291 100644 (file)
     /* Postcode. */
     echo "<tr>\n";
     echo "  <td>Postcode</td>\n";
-    echo "  <td>"; input("postcode", $address->getPostcode()); echo get_address_map_link($address); echo "</td>\n";
+    $postcode = $address->getPostcode();
+    if (validate_postcode($postcode)) {
+      echo "  <td>"; input("postcode", $postcode); echo get_address_map_link($address); echo "</td>\n";
+    }
+    else {
+      echo "  <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
+    }
     echo "</tr>\n";
 
     /* Telephone. */
     /* Get address. */
     $area_id = $_POST['area_id'];
     $line = $_POST['address'];
-    $postcode = $_POST['postcode'];
+    $postcode = trim($_POST['postcode']);
+    if ($postcode) {
+      $postcode = format_postcode($_POST['postcode'], true);
+      if (! $postcode) return false;
+    }
     $q = new AddressQuery;
     /* XXX: Finding by area properly? */
     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();
index 73ea464..0f2e0e7 100644 (file)
     echo "Day: <input name=\"$name" . "_d\" value=\"$d\" size=2 maxlen=2> ";
   }
 
+  function validate_postcode($postcode, &$outward = null, &$inward = null) {
+    /*
+      Valid postcode formats (BS766):
+
+        AN NLL
+        ABN NLL
+        ANN NLL
+        ABNN NLL
+        ABND NLL
+        ANC NLL
+
+      Where N is a number; A is a letter not including Q, V, X;
+      B is a letter not including I, J, Z; C is a letter from the set
+      ABCDEFGHJKSTUW; D is a letter from the set ABEHMNPRVWXY;
+      L is a letter from the set ABDEFGHJLNPQRSTUWXYZ.
+
+      The postcode GIR 0AA is also valid.
+    */
+    $outward = $inward = null;
+
+    /* Treat blank as valid for convenience. */
+    $postcode = trim($postcode);
+    if (! $postcode) return true;
+
+    $A = '[ABCDEFGHIJKLMNOPRSTUWYZ]';
+    $B = '[ABCDEFGHKLMNOPQRSTUVWXY]';
+    $C = '[ABCDEFGHJKSTUW]';
+    $D = '[ABEHMNPRVWXY]';
+    $L = '[ABDEFGHJLNPQRSTUWXYZ]';
+    $N = '\d';
+    if (! preg_match("/^($A$N|$A$B$N|$A$N$N|$A$B$N$N|$A$B$N$D|$A$N$C|GIR)\s*($N$L$L)$/", $postcode, $m)) return false;
+    if ($m[1] == "GIR" && $m[2] != "0AA") return false;
+    list($ignored, $outward, $inward) = $m;
+    return true;
+  }
+
+  function format_postcode($postcode, $complain = true) {
+    if (validate_postcode($postcode, $outward, $inward)) {
+      return "$outward $inward";
+    }
+    if ($complain) {
+      echo "<p>Invalid postcode!</p>\n";
+      return null;
+    }
+  }
+
   function get_small_link() {
     /* Args are <alt text>, <format>, [<stuff> ...] */
     $args = func_get_args();
index 12f27cf..3d8e8d5 100644 (file)
     /* Postcode. */
     echo "<tr>\n";
     echo "  <td>Postcode</td>\n";
-    echo "  <td>"; input("postcode", $address->getPostcode()); echo get_address_map_link($address); echo "</td>\n";
+    $postcode = $address->getPostcode();
+    if (validate_postcode($postcode)) {
+      echo "  <td>"; input("postcode", $postcode); echo get_address_map_link($address); echo "</td>\n";
+    }
+    else {
+      echo "  <td>"; input("postcode", $address->getPostcode()); echo " (invalid)</td>\n";
+    }
     echo "</tr>\n";
 
     /* Telephone. */
 
     /* Get address. */
     $line = $_POST['address'];
-    $postcode = $_POST['postcode'];
+    $postcode = trim($_POST['postcode']);
+    if ($postcode) {
+      $postcode = format_postcode($_POST['postcode'], true);
+      if (! $postcode) return false;
+    }
     $q = new AddressQuery;
     /* XXX: Finding by area properly? */
     $address = $q->filterByAreaId($area_id)->filterByLine($line)->filterByPostcode($postcode)->findOneOrCreate();