Posted By

valcartei on 07/06/07


Tagged

forms fields validation postcode


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

asifjavedall


Postcode validation


 / Published in: PHP
 

a function to check the validity of the postcode according to UK rules

  1. <?php
  2. /*==============================================================================
  3. Application: Utiity Function
  4. Author: John Gardner
  5.  
  6. Version: V1.0
  7. Date: 25th December 2004
  8. Description: Used to check the validity of a UK postcode
  9.  
  10. Version: V2.0
  11. Date: 8th March 2005
  12. Description: BFPO postcodes implemented.
  13.   The rules concerning which alphabetic characters are alllowed in
  14.   which part of the postcode were more stringently implementd.
  15.  
  16. Parameters: $postcode - postcodeto be checked. This is returned reformatted
  17.   if valid.
  18.  
  19. This function checks the value of the parameter for a valid postcode format. The
  20. space between the inward part and the outward part is optional, although is
  21. inserted if not there as it is part of the official postcode.
  22.  
  23. The functions returns a value of false if the postcode is in an invalid format,
  24. and a value of true if it is in a valid format. If the postcode is valid, the
  25. parameter is loaded up with the postcode in capitals, and a space between the
  26. outward and the inward code to conform to the correct format.
  27.  
  28. Example call:
  29.  
  30.   if (!checkPostcode($postcode) ) {
  31.   echo 'Invalid postcode <br>';
  32.   }
  33.  
  34. ------------------------------------------------------------------------------*/
  35. function checkPostcode (&$toCheck) {
  36.  
  37. // Permitted letters depend upon their position in the postcode.
  38. $alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1
  39. $alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
  40. $alpha3 = "[abcdefghjkstuw]"; // Character 3
  41. $alpha4 = "[abehmnprvwxy]"; // Character 4
  42. $alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5
  43.  
  44. // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  45. $pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';
  46.  
  47. // Expression for postcodes: ANA NAA
  48. $pcexp[1] = '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';
  49.  
  50. // Expression for postcodes: AANA NAA
  51. $pcexp[2] = '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';
  52.  
  53. // Exception for the special postcode GIR 0AA
  54. $pcexp[3] = '^(gir)(0aa)$';
  55.  
  56. // Standard BFPO numbers
  57. $pcexp[4] = '^(bfpo)([0-9]{1,4})$';
  58.  
  59. // c/o BFPO numbers
  60. $pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$';
  61.  
  62. // Load up the string to check, converting into lowercase and removing spaces
  63. $postcode = strtolower($toCheck);
  64. $postcode = str_replace (' ', '', $postcode);
  65.  
  66. // Assume we are not going to find a valid postcode
  67. $valid = false;
  68.  
  69. // Check the string against the six types of postcodes
  70. foreach ($pcexp as $regexp) {
  71.  
  72. if (ereg($regexp,$postcode, $matches)) {
  73.  
  74. // Load new postcode back into the form element
  75. $toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);
  76.  
  77. // Take account of the special BFPO c/o format
  78. $toCheck = ereg_replace ('C\/O', 'c/o ', $toCheck);
  79.  
  80. // Remember that we have found that the code is valid and break from loop
  81. $valid = true;
  82. break;
  83. }
  84. }
  85.  
  86. // Return with the reformatted valid postcode in uppercase if the postcode was
  87. // valid
  88. if ($valid){return true;} else {return false;};
  89. }
  90. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: johnsie on April 21, 2009

Hello. I had a problem validating SA19 8MD with this. I'm currently trying to find out if that is a valid postcode. Any ideas?

Posted By: johnsie on April 21, 2009

ps. I ran around 20,000 postcodes through this and that was the only possibly right looking one that came out as invalid.

Posted By: u24 on January 11, 2013

I know that SA48 7ED is a valid UK postcode.

You need to login to post a comment.