Last active
June 8, 2016 01:11
-
-
Save joshluongo/7704922219b078b8cf95686ef5912bf2 to your computer and use it in GitHub Desktop.
Australian Postcode to State - Demo https://jsfiddle.net/7o772r4s/4/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Data from https://en.wikipedia.org/wiki/Postcodes_in_Australia | |
function postcodeToState(input) { | |
// Convert. | |
var postcode = parseInt(input); | |
// Is valid. | |
if (isNaN(postcode) || postcode > 9999 || postcode < 100) { | |
return "Invalid postcode"; | |
} | |
// Handle edge states | |
switch (postcode) { | |
case 0872: | |
return "QLD, SA, WA"; | |
case 2540: | |
case 2620: | |
case 2611: | |
return "ACT, NSW"; | |
case 2611: | |
case 3500: | |
case 3585: | |
case 3586: | |
case 3644: | |
case 3707: | |
return "NSW"; | |
case 2406: | |
case 4380: | |
case 4377: | |
return "NSW, QLD"; | |
case 3691: | |
return "NSW, VIC"; | |
// Islands | |
case 2889: | |
return "Norfolk Island"; | |
case 6798: | |
return "Christmas Island"; | |
case 6799: | |
return "Cocos (Keeling) Islands"; | |
// Antarctica | |
case 7151: | |
return "Antarctica"; | |
default: | |
break; | |
} | |
// Blocks. | |
if ((postcode >= 1000 && postcode <= 2599) || | |
(postcode >= 2619 && postcode <= 2899) || | |
(postcode >= 2921 && postcode <= 2999)) { | |
return "NSW"; | |
} else if ((postcode >= 0200 && postcode <= 0299) || | |
(postcode >= 2600 && postcode <= 2618) || | |
(postcode >= 2900 && postcode <= 2920)) { | |
return "ACT"; | |
} else if ((postcode >= 3000 && postcode <= 3999) || | |
(postcode >= 8000 && postcode <= 8999)) { | |
return "VIC"; | |
} else if ((postcode >= 4000 && postcode <= 4999) || | |
(postcode >= 9000 && postcode <= 9999)) { | |
return "QLD"; | |
} else if ((postcode >= 5000 && postcode <= 5999)) { | |
return "SA"; | |
} else if ((postcode >= 7000 && postcode <= 7999)) { | |
return "TAS"; | |
} else if ((postcode >= 0800 && postcode <= 0999)) { | |
return "NT"; | |
} | |
return "Unknown" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment