Skip to content

Instantly share code, notes, and snippets.

@wamith
Last active May 4, 2019 06:22
Show Gist options
  • Save wamith/e6325ec7039a7acf8d3ca9e00b8403b8 to your computer and use it in GitHub Desktop.
Save wamith/e6325ec7039a7acf8d3ca9e00b8403b8 to your computer and use it in GitHub Desktop.
/* UK Postcode Pattern
* < Outward Code > < Inward Code>
* <AREA><DISTRICT> <SECTOR><UNIT>
* AREA = 1 or 2 chars
* DISTRICT = (one or 2 digits) or (digit then letter)
* Space (optional)
* SECTOR = digit
* UNIT = 2 chars
*
* $postcode_regex = '/^([A-Z]{1,2})([0-9]{1,2}|[0-9][A-Z])\s?([0-9])([A-Z]{2})$/i';
*/
$postcode_regex='/^'. // Start of postcode
'([A-Z]{1,2})'. // AREA = 1 or 2 chars
'([0-9]{1,2}|[0-9][A-Z])'. // DISTRICT = (one or 2 digits) or (digit then letter)
'\s?'. // Space (optional)
'([0-9])'. // SECTOR = digit
'([A-Z]{2})'. // UNIT = 2 chars
'$/'; // End of Postcode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment