Converting PHP to Javascript for State Extraction by numeric value Australian State Names
<p>I cant even remember when i originally wrote this php function, but now i need to have it in javascript, should not be much change to it to get it working. </p>
<p>The original function takes an australian postcode (2000) and returns the state value (NSW, VIC, etc). Not sure if its 100% accurate. </p>
<p>Australian state and postcode lookup.</p>
<h3>Testing</h3>
<p>Looks like just one function is missing so far:</p>
<p><img src="https://i.imgur.com/uTOuKbY.png" /></p>
<p>What is the javascript for isnumeric? <a href="https://stackoverflow.com/questions/9716468/pure-javascript-a-function-like-jquerys-isnumeric" target="blank" rel="noopener">Stack overflow</a> to the rescue here. </p>
<h4>Javascript</h4>
<pre><code class="javascript hljs">function isnumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}</code></pre>
<p>Yep just replace that one function, and its working... too easy.</p>
<p><img src="https://i.imgur.com/4LETYoe.png" /></p>
<h3>Usage</h3>
<p>Just use : console.log(getstateau("2000"));</p>
<p>or</p>
<p>$state = getstate_au("2000");</p>
<p>passing in your selected postcode. </p>
Javascript
// i wonder if it will just wor, probably not... lets see.
function get_state_au($postcode) {
$state = false;
if(!is_numeric($postcode)) { return false; }
if(($postcode >= 2000 && $postcode <= 2899)) { $state = "NSW"; } // nsw should be checked 1st as act may override it
if(($postcode >= 2600 && $postcode <= 2618)) { $state = "ACT"; }
if(($postcode >= 2900 && $postcode <= 2999)) { $state = "ACT"; }
if(($postcode >= 800 && $postcode <= 900)) { $state = "NT"; }
if(($postcode >= 4000 && $postcode <= 4999)) { $state = "QLD"; }
if(($postcode >= 5000 && $postcode <= 5999)) { $state = "SA"; }
if(($postcode >= 7000 && $postcode <= 7999)) { $state = "TAS"; }
if(($postcode >= 3000 && $postcode <= 3999)) { $state = "VIC"; }
if(($postcode >= 6000 && $postcode <= 6999)) { $state = "WA"; }
return $state;
}
function is_numeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
console.log(get_state_au("2000"));PHP
// get a state name from an australian postcode
function get_state_au($postcode) {
$state = false;
if(!is_numeric($postcode)) { return false; }
if(($postcode >= 2000 && $postcode <= 2899)) { $state = "NSW"; } // nsw should be checked 1st as act may override it
if(($postcode >= 2600 && $postcode <= 2618)) { $state = "ACT"; }
if(($postcode >= 2900 && $postcode <= 2999)) { $state = "ACT"; }
if(($postcode >= 800 && $postcode <= 900)) { $state = "NT"; }
if(($postcode >= 4000 && $postcode <= 4999)) { $state = "QLD"; }
if(($postcode >= 5000 && $postcode <= 5999)) { $state = "SA"; }
if(($postcode >= 7000 && $postcode <= 7999)) { $state = "TAS"; }
if(($postcode >= 3000 && $postcode <= 3999)) { $state = "VIC"; }
if(($postcode >= 6000 && $postcode <= 6999)) { $state = "WA"; }
return $state;
}
echo get_state_au("3000");
echo "<br>";
echo get_state_au("2000");