Posted By

Scooter on 08/23/08


Tagged

regular expressions


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Wardy
silentbob84


isValidPostCode


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com/2008/08/isvalidpostcode.html

  1. <?php
  2. /*
  3.   Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  4.  
  5.   This work is licensed under the Creative Commons Attribution License. To view
  6.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  7.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  8.   94305, USA.
  9.   */
  10.  
  11. // Checks the validity of the format of a supplied UK postcode.
  12. // Completely accurate validation is only possible by attempting to deliver mail to the address, and verifying with the recipient.
  13. // Supports all six common postcode formats, Girobank, Father Christmas, British Forces Post Office, and overseas territories.
  14. function isValidPostCode($postCode)
  15. {
  16. $pattern = "/^([A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])\ [0-9][ABD-HJLNP-UW-Z]{2}|(GIR\ 0AA)|(SAN\ TA1)|(BFPO\ (C\/O\ )?[0-9]{1,4})|((ASCN|BBND|[BFS]IQQ|PCRN|STHL|TDCU|TKCA)\ 1ZZ))$/i";
  17. return (preg_match($pattern, trim($postCode)) > 0) ? true : false;
  18. }
  19. ?>

Report this snippet  

You need to login to post a comment.