RE: [PHP] UK Postcodes Format
| From: | Angela K Hilton | Date: | Tue, 17 Feb 2004 13:02:49 +0000 |
| Subject: | RE: [PHP] UK Postcodes Format | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-177937@lists.php.net to get a copy of this message | ||
I 'Think' there are also different formats for military bases here and
overseas.
[this is a faint memory from validating post codes in a database some
years ago]
A
***********************************
Angela K Hilton
Web & E-Learning Officer
ISD, UMIST
Tel: 0161 306 3109
***********************************
-----Original Message-----
From: Ford, Mike [LSS] [mailto:M.Ford@leedsmet.ac.uk]
Sent: 17 February 2004 12:15
To: 'Roddie Grant'; Shaun; php-general@lists.php.net
Subject: RE: [PHP] UK Postcodes Format
On 17 February 2004 10:43, Roddie Grant wrote:
> on 17/2/04 10:25 am, Shaun at shaun@mania.plus.com wrote:
>
> > Hi,
> >
> > does anyone know the format of the postcodes in the UK so I can
> > keep my database accurate?
> >
> > Thanks
>
>
> Try
> http://javascript.internet.com/forms/uk-postcode-validation.html
Wow, at a glance I'd say that looks like pretty crappy JavaScript, and
also not a totally accurate UK postcode checker.
The only guaranteed way to fully validate a UK postcode is to purchase
the Royal Mail's Postcode Address File (PAF) (and subscribe to its
updates!), but you can validate the format fairly closely with some
simple rules.
For example, here's a regexp that matches valid UK postcode patterns:
/[A-Z]{1,2}[0-9][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{1,2}/
or, in English:
* an "outward" code consisting of:
- 1 or 2 alphabetic characters
- followed by 1 or 2 digits, or 1 digit and 1 letter
* a space
* in "inward" code consisting of:
- 1 digit
- followed by 2 letters, but not including C, I, K, M, O or V.
(The special code GIR 0AA also exists for the headquarters of the
formerly-Government-owned Girobank, now part of the Alliance &
Leicester, and is the only one that doesn't match these rules).
Although this will check for potentially valid formats, there are a
number of other checks you can make if you wish:
- the initial 1 or 2 letters come from a restricted set of about 120,
indicating the main sorting office which handles mail for an area --
generally they correspond to a large town or city (B - Birmingham, L -
Liverpool, LS - Leeds, etc.) but London ones are taken from the much
older London postal district names (E - East, EC - East Central, etc.)
and a few represent more general area names (ZE - Shetland Islands, for
example. (I could probably produce a list of these fairly easily.)
- The range of values in the rest of the "outward" code is likewise
restricted; the range is different for each area, with many restricted
to 20 or less, and none uses the full set of 1-99; however, higher
values are often used for special purposes (98 and 99 for PO Boxes, for
example), and most of the larger areas have multiple disjoint ranges.
- The only postcodes which have a final letter in the "outward" portion
of the code are some of the London districts (off the top of my head,
EC, SW, W and WC only, I think, and maybe not all of those!).
- For each "outward" code, there may also be restrictions on the range
of "inward" codes, and these will again be different for each area (for
example, for my home outward code all inward codes begin with a 2; in
the neighbouring outward code, all inward codes begin with a 3). These
are so numerous and diverse that they're not really worth bothering
about unless you're going the full-blown PAF route.
Anyway, this is probably already far more information than you were
expecting, so I'll shut up now!
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php