Jump to content

Talk:Postal codes in South Africa

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Untitled

[edit]

So, I haven't seen/found any code to determine which province a specific code would fall under, so I thought I'd add this here in the discussion page. This is just some JavaScript I hacked together for a project I'm working on, so it might not be the most effective way of accomplishing said goal. If you want to re-post this somewhere more appropriate (or use it or whatever), please feel free to do so.

function GetProvinceForPostalCode(postalCode)
{
	try
	{
		var postalCodes = new Array();

		postalCodes['Gauteng'] = new Array("0001|0204","1400|1699","1438|1444","1700|1799","1800|1870","1871|1990","2000|2199");
		postalCodes['Mpumalanga'] = new Array("0205|0698","1000|1199","1200|1399","2200|2494");
		postalCodes['Limpopo'] = new Array("0699|0999");
		postalCodes['North West'] = new Array("2495|2519","2520|2709","2710|2899");
		postalCodes['KwaZulu-Natal'] = new Array("2900|3199","3200|3309","3310|3599","3600|3799","3800|3990","3991|4179","4180|4299","4300|4641","4642|4730","4740|4799");
		postalCodes['Eastern Cape'] = new Array("4735|4739","4800|4899","4920|5049","5050|5199","5200|5750","5751|6499");
		postalCodes['Western Cape'] = new Array("6500|6699","6700|6899","6900|7099","7100|8179");
		postalCodes['Northern Cape'] = new Array("8180|8299","8300|8799","8800|8999");
		postalCodes['Free State'] = new Array("9300|9409","9410|9699","9700|9999");

		var selectedProvince = '';

		for (i in postalCodes) 
		{ 
			var ranges = postalCodes[i].toString().split(',');
			for (var j=0; j<ranges.length; j++)
			{
				var codes = ranges[j].split('|');
				if (postalCode >= codes[0] && postalCode <= codes[1])
				{
					selectedProvince = i;
				}
			}
		}
		return selectedProvince
	}
	catch(e)
	{
		alert("GetProvinceForPostalCode error: " + e.message);
	}
}