[ic] Formatting Zip for UK - Change to Upper Case and Add aSpace??

AS news1 at dsl.pipex.com
Thu Nov 6 21:05:27 EST 2003

>Andrew Smith [a_w_smith at ntlworld.com] wrote:
>> I would like to format the zip field to display correctly for UK
>> postcodes on the order_view.html page.
>> UK postcodes can be of the following formats:-
>> Where A=Alpha and N=Numeric.
>Your validation rules are incorrect.  My post code (RH9 8AX) does not
>conform to the above, neither does the BBC Broadcasting House in
>London (W1A 1AA).
>> At the moment all my zip/postcodes appear in lower case letters with
>> space, even if a space is entered at checkout.
>> Is there some piece of script I could use to:-
>> 1) Convert all Alpha to uppercase
>> 2) Insert a space at the point 3 characters before the end.
>> Could this be done in Perl?
>Yes, this could be done using Perl.  I believe the following to be
>an accurate summary of the UK post code validation rules, so should
>help to get you started:
>  1. The total length must be 5, 6 or 7 characters, excluding the
>  2. A single space must be included in the post code.
>  2. The inward code, the part to the right of the space, must always
>     be 3 characters.
>  3. The first character of the inward code must be numeric.
>  4. The second and third characters of the inward code must be alpha.

Usually, but not always true, eg WC1 or EC3 in London. 

>  5. The outward code, the part to the left of the space, can be 2, 3
>     or 4 characters.
>  6. The first character of the outward code must be alpha.

Hi all,

Thanks for the comments, I wasn't actually trying to validate the

ALL uk postcodes have a space before the last 3 characters, all I want
to do is format the post code so all the letters are capitals and there
is a space before the last 3 characters.

The perl lines to do this are:-

s/(...$)/ ${1}/g; and tr/a-z/A-Z/;

But I cannot work out how to re-format the 'zip' variable with this
code??? Any ideas..


More information about the interchange-users mailing list