Uk postcode

From Ezee.co.uk

Jump to: navigation, search
# Main function  validate_uk_postcode($postcode)
#
# All code Copyright Karl Gray (c) 2000,2001
#
# It will reformat the postcode and present it in a uniform format acceptable to Nominet
# If the postcode is invalid it returns INVALID, if it is good it returns the postcode properly formated
#
# Here are the various regular expressions we have used.
#
# Our original one, Breaks on unusual london postcodes
# ^[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}$
#
# From nominet, no start or termination, users can enter guff at beginning or end
# [A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} [0-9][A-Z]{2}
#
# Our new one, Fixed Nominet one to prevent pre or post garbage.
# ^[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} [0-9][A-Z]{2}$
# Now covers All weird london postcodes

### FUNCTIONS ###

function tidy_postcode($postcode) {
       $postcode=strtoupper($postcode);
       $postcode=trim($postcode);
       if (!eregi(" ",$postcode)) {
               $len=strlen($postcode);
               $postcode[$len]=$postcode[$len-1];
               $postcode[$len-1]=$postcode[$len-2];
               $postcode[$len-2]=$postcode[$len-3];
               $postcode[$len-3]=" ";
               }
       return ($postcode);
       }

function validate_uk_postcode($postcode) {

       # Change it all to uppercase for readability and format checks
       $postcode=strtoupper($postcode);

       # Reformat postcode,  space is now in right place for Nominet orders
       $postcode=tidy_postcode($postcode);

       # Check if postcode is valid
       if (!eregi("^[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} [0-9][A-Z]{2}$",$postcode)) {
       	return("INVALID");
               }
       # YES! got one right :-)
       return($postcode);
	}

Example error text
The postcode $postcode you entered is not a Valid UK postcode
We can only accept orders if a valid UK address is provided
If you feel we are in error please contact support who will be able to assist
HINTS!!
Common mistakes that are made are as follows
Uusing a letter O straight after the space in your postcode instead of a number 0
Using a Number 0 instead of a Letter O
Using other characters such a - or terminating the postcode with a full stop \" . \" Please only use the characters that are part of your postcode

Personal tools