Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Created April 21, 2009 04:42
Show Gist options
  • Save rubysolo/98944 to your computer and use it in GitHub Desktop.
Save rubysolo/98944 to your computer and use it in GitHub Desktop.
var ZipRanges = {
ranges: {
'AK':'99500-99929',
'AL':'35000-36999',
'AR':'71600-72999,75502-75505'
// ...
},
toState: function(zip_code) {
var z = parseInt(zip_code);
var s = null;
$.each(ZipRanges.ranges, function(state, zip_range) {
var chunks = zip_range.split(/\,/);
$.each(chunks, function(i, chunk) {
var range = chunk.split(/\-/);
var start = parseInt(range[0]);
var finish = parseInt(range[1]);
if ((start <= z) && (finish >= z)) {
s = state;
return false;
}
});
if (s != null) return false;
});
if (s != null) return s;
return "Invalid Zip";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment