Many web forms and other systems ask for the postcode as well as the State. In Australia, it is a simple relationship and does not currently require a complex lookup table, since each State or Territory has a fixed numeric range of postcodes. By executing an IF-ELSE-ELSE-ELSE... one can easily determine the State. It also deals with the fact that most web forms will have data entered as strings, and attempts to parse it, or defaults to one of the States.
In order to reduce the number of fields for our clients to fill out, we wanted a way to collect the most detailed information in the least number of form fields. In Australia, the Postcode is closely related to the State or Territory, and one could go for a full database lookup solution. However, a simpler solution given here is just to use the fact that Australian Postcodes are numeric, and that given numeric ranges are tied to specific States and Territories.
The Australian Postal Service (called Australia Post) maintains the Postcodes register, and from their website and others, it is possible to look up the Postcode from an address, or a town name. However, there are times when the Postcode may be collected for one reason or another, such as to determine the closest store for a given business, and often the State is also requested. But we all know that more information required of our users just means less completions.
The Postcode in Australia is a 4 digit number, written as "0000" through to "9999", for example, Sydney has a Postcode of 2000.
Using the code
This is a simple function, written in C# but easily translatable to other languages.
The only parameter required is the
string for the
Postcode, which is likely to be what someone will have typed into a "
Postcode" field on a form. It does not need to be a number that is entered.
The function returns a
string being the conventional abbreviation used for one of the Australian States or Territories.
We are hard-coding to default to "
NSW" if the string cannot be parsed as a number, or if the number does not fit within the ranges of
static string stateFromPostcode(string postcodeString)
string returnState = "NSW";
int postcode = 0;
bool result = Int32.TryParse(postcodeString, out postcode);
if (result == true)
if ((postcode > 799) && (postcode < 1000)) returnState = "NT";
else if (postcode < 2000) returnState = "NSW";
else if (((postcode > 2599) && (postcode < 2619)) ||
((postcode > 2899) && (postcode < 3000))) returnState = "ACT";
else if (postcode < 3000) returnState = "NSW";
else if (postcode < 4000) returnState = "VIC";
else if (postcode < 5000) returnState = "QLD";
else if (postcode < 6000) returnState = "SA";
else if (postcode < 7000) returnState = "WA";
else if (postcode < 8000) returnState = "TAS";
else returnState = "NSW";
} else returnState = "NSW";
Points of Interest
See the Postcodes Australia website for a table stating the Postcode numeric ranges for Australian States and Territories.
- 15th September, 2021: First version published (with a couple of typos!)