Postcodes

From Leaky
Jump to: navigation, search

Postcode validation by regex:

 if ($postcode =~ /^([A-PR-UWYZ][0-9][0-9]?|[A-PR-UWYZ][A-HK-Y][0-9][0-9]?|[A-PR-UWYZ][0-9][A-HJKPSTUW]|[A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]) ?([0-9][ABD-HJLNP-UWXYZ]{2})$/i) {
   $postcode = uc("$1 $2");
 } else {
   # postcode invalid format
 }

Latest ONS Postcode Directory download can be found here:

Easy HTTP API to reference ONS data: