Revision: 26764
Updated Code
at May 14, 2010 06:04 by adrianparr
Updated Code
var inputPostcode:String = " e c 1 r 5 d w "; trace("inputPostcode: "+inputPostcode); var checkedPostcode:String = validateUkPostcode(inputPostcode).toUpperCase(); if (checkedPostcode == "") { trace("No valid postcode was found"); } else { trace("checkedPostcode: "+checkedPostcode); } function validateUkPostcode(str:String):String { var returnString:String; var withNoSpaces:String = str.split(" ").join(""); var outwardCode:String = withNoSpaces.substring(0, withNoSpaces.length-3); var inwardCode:String = withNoSpaces.substring(withNoSpaces.length-3, withNoSpaces.length); var formatted:String = outwardCode + " " + inwardCode; str = formatted.replace(/^\s+|\s+$/g, ""); var pattern:RegExp = /[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}/i; var result:Object = pattern.exec(str); if(result == null) { returnString = ""; } else { returnString = result[0]; } return returnString; } // OUTPUT // inputPostcode: e c 1 r 5 d w // checkedPostcode: EC1R 5DW
Revision: 26763
Updated Code
at May 14, 2010 05:23 by adrianparr
Updated Code
function formattedPostcode($numStr:String):String { var withNoSpaces:String = $numStr.split(" ").join(""); var outwardCode:String = withNoSpaces.substring(0, withNoSpaces.length-3); var inwardCode:String = withNoSpaces.substring(withNoSpaces.length-3, withNoSpaces.length); var formatted = outwardCode + " " + inwardCode; formatted = formatted.replace(/^\s+|\s+$/g, ""); return formatted; } var postcode:String = " A A 9 99 A A "; trace(formattedPostcode(postcode)); // OUTPUT // AA99 9AA
Revision: 26762
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 10, 2010 08:02 by adrianparr
Initial Code
function formattedPostcode($numStr:String):String { var withNoSpaces:String = $numStr.split(" ").join(""); var outwardCode:String = withNoSpaces.substring(0, withNoSpaces.length-3); var inwardCode:String = withNoSpaces.substring(withNoSpaces.length-3, withNoSpaces.length); var formatted = outwardCode + " " + inwardCode; return formatted; } var postcode:String = " A A 9 99 A A "; trace(formattedPostcode(postcode)); // OUTPUT // AA99 9AA
Initial URL
http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
Initial Description
This code removes all the spaces and then inserts a single space so that the there are only 3 characters on the right-hand side (the inward code).
Initial Title
AS3 Format a UK Postcode (eg. AA99 9AA)
Initial Tags
format, uk
Initial Language
ActionScript 3