How can I quickly determine the State for a given zipcode?
Asked Answered
I

9

23

I don't need the city or address, just the state. And I don't want to make an API call if possible. The priority is a light-weight solution, ideally just Javascript.

I have a user-input zipcode, and I want to display a paragraph of text depending on the state that the zipcode is in. I know it's much more complicated to lookup City and State, and for that an API such as the one the USPS exposes is probably best. But to just match state, perhaps on just the first three numbers, the solution (I think) should be easy and lightweight.

Javascript ideally. PHP could also work.

Invisible answered 3/3, 2015 at 0:12 Comment(5)
The free one of these has the state information - unitedstateszipcodes.org/zip-code-databaseFanchette
You need a database of zipcodes and their respective states. You can get one from the USPS.Yeung
The duplicate for this question has only one upvoted answer, and it is a link to a deleted question.Brycebryn
This has been incorrectly marked as duplicate.Soporific
You can check thezipcodes.com as well.Spandrel
I
68

US zipcode data is in fact stable enough that you can do this without hitting an API or a database if you only need the State (not the City or anything else).

Here's a lightweight JS solution that takes a zipcode (as a string), determines the proper US state, and returns the state.

function getState(zipString) {

  /* Ensure param is a string to prevent unpredictable parsing results */
  if (typeof zipString !== 'string') {
      console.error('Must pass the zipcode as a string.');
      return;
  }

  /* Ensure we have exactly 5 characters to parse */
  if (zipString.length !== 5) {
      console.error('Must pass a 5-digit zipcode.');
      return;
  }

  /* Ensure we don't parse strings starting with 0 as octal values */
  const zipcode = parseInt(zipString, 10);

  let st;
  let state;

  /* Code cases alphabetized by state */
  if (zipcode >= 35000 && zipcode <= 36999) {
      st = 'AL';
      state = 'Alabama';
  } else if (zipcode >= 99500 && zipcode <= 99999) {
      st = 'AK';
      state = 'Alaska';
  } else if (zipcode >= 85000 && zipcode <= 86999) {
      st = 'AZ';
      state = 'Arizona';
  } else if (zipcode >= 71600 && zipcode <= 72999) {
      st = 'AR';
      state = 'Arkansas';
  } else if (zipcode >= 90000 && zipcode <= 96699) {
      st = 'CA';
      state = 'California';
  } else if (zipcode >= 80000 && zipcode <= 81999) {
      st = 'CO';
      state = 'Colorado';
  } else if ((zipcode >= 6000 && zipcode <= 6389) || (zipcode >= 6391 && zipcode <= 6999)) {
      st = 'CT';
      state = 'Connecticut';
  } else if (zipcode >= 19700 && zipcode <= 19999) {
      st = 'DE';
      state = 'Delaware';
  } else if (zipcode >= 32000 && zipcode <= 34999) {
      st = 'FL';
      state = 'Florida';
  } else if ( (zipcode >= 30000 && zipcode <= 31999) || (zipcode >= 39800 && zipcode <= 39999) ) {
      st = 'GA';
      state = 'Georgia';
  } else if (zipcode >= 96700 && zipcode <= 96999) {
      st = 'HI';
      state = 'Hawaii';
  } else if (zipcode >= 83200 && zipcode <= 83999 && zipcode != 83414) {
      st = 'ID';
      state = 'Idaho';
  } else if (zipcode >= 60000 && zipcode <= 62999) {
      st = 'IL';
      state = 'Illinois';
  } else if (zipcode >= 46000 && zipcode <= 47999) {
      st = 'IN';
      state = 'Indiana';
  } else if (zipcode >= 50000 && zipcode <= 52999) {
      st = 'IA';
      state = 'Iowa';
  } else if (zipcode >= 66000 && zipcode <= 67999) {
      st = 'KS';
      state = 'Kansas';
  } else if (zipcode >= 40000 && zipcode <= 42999) {
      st = 'KY';
      state = 'Kentucky';
  } else if (zipcode >= 70000 && zipcode <= 71599) {
      st = 'LA';
      state = 'Louisiana';
  } else if (zipcode >= 3900 && zipcode <= 4999) {
      st = 'ME';
      state = 'Maine';
  } else if (zipcode >= 20600 && zipcode <= 21999) {
      st = 'MD';
      state = 'Maryland';
  } else if ( (zipcode >= 1000 && zipcode <= 2799) || (zipcode == 5501) || (zipcode == 5544 ) ) {
      st = 'MA';
      state = 'Massachusetts';
  } else if (zipcode >= 48000 && zipcode <= 49999) {
      st = 'MI';
      state = 'Michigan';
  } else if (zipcode >= 55000 && zipcode <= 56899) {
      st = 'MN';
      state = 'Minnesota';
  } else if (zipcode >= 38600 && zipcode <= 39999) {
      st = 'MS';
      state = 'Mississippi';
  } else if (zipcode >= 63000 && zipcode <= 65999) {
      st = 'MO';
      state = 'Missouri';
  } else if (zipcode >= 59000 && zipcode <= 59999) {
      st = 'MT';
      state = 'Montana';
  } else if (zipcode >= 27000 && zipcode <= 28999) {
      st = 'NC';
      state = 'North Carolina';
  } else if (zipcode >= 58000 && zipcode <= 58999) {
      st = 'ND';
      state = 'North Dakota';
  } else if (zipcode >= 68000 && zipcode <= 69999) {
      st = 'NE';
      state = 'Nebraska';
  } else if (zipcode >= 88900 && zipcode <= 89999) {
      st = 'NV';
      state = 'Nevada';
  } else if (zipcode >= 3000 && zipcode <= 3899) {
      st = 'NH';
      state = 'New Hampshire';
  } else if (zipcode >= 7000 && zipcode <= 8999) {
      st = 'NJ';
      state = 'New Jersey';
  } else if (zipcode >= 87000 && zipcode <= 88499) {
      st = 'NM';
      state = 'New Mexico';
  } else if ( (zipcode >= 10000 && zipcode <= 14999) || (zipcode == 6390) || (zipcode == 501) || (zipcode == 544) ) {
      st = 'NY';
      state = 'New York';
  } else if (zipcode >= 43000 && zipcode <= 45999) {
      st = 'OH';
      state = 'Ohio';
  } else if ((zipcode >= 73000 && zipcode <= 73199) || (zipcode >= 73400 && zipcode <= 74999) ) {
      st = 'OK';
      state = 'Oklahoma';
  } else if (zipcode >= 97000 && zipcode <= 97999) {
      st = 'OR';
      state = 'Oregon';
  } else if (zipcode >= 15000 && zipcode <= 19699) {
      st = 'PA';
      state = 'Pennsylvania';
  } else if (zipcode >= 300 && zipcode <= 999) {
      st = 'PR';
      state = 'Puerto Rico';
  } else if (zipcode >= 2800 && zipcode <= 2999) {
      st = 'RI';
      state = 'Rhode Island';
  } else if (zipcode >= 29000 && zipcode <= 29999) {
      st = 'SC';
      state = 'South Carolina';
  } else if (zipcode >= 57000 && zipcode <= 57999) {
      st = 'SD';
      state = 'South Dakota';
  } else if (zipcode >= 37000 && zipcode <= 38599) {
      st = 'TN';
      state = 'Tennessee';
  } else if ( (zipcode >= 75000 && zipcode <= 79999) || (zipcode >= 73301 && zipcode <= 73399) ||  (zipcode >= 88500 && zipcode <= 88599) ) {
      st = 'TX';
      state = 'Texas';
  } else if (zipcode >= 84000 && zipcode <= 84999) {
      st = 'UT';
      state = 'Utah';
  } else if (zipcode >= 5000 && zipcode <= 5999) {
      st = 'VT';
      state = 'Vermont';
  } else if ( (zipcode >= 20100 && zipcode <= 20199) || (zipcode >= 22000 && zipcode <= 24699) || (zipcode == 20598) ) {
      st = 'VA';
      state = 'Virginia';
  } else if ( (zipcode >= 20000 && zipcode <= 20099) || (zipcode >= 20200 && zipcode <= 20599) || (zipcode >= 56900 && zipcode <= 56999) ) {
      st = 'DC';
      state = 'Washington DC';
  } else if (zipcode >= 98000 && zipcode <= 99499) {
      st = 'WA';
      state = 'Washington';
  } else if (zipcode >= 24700 && zipcode <= 26999) {
      st = 'WV';
      state = 'West Virginia';
  } else if (zipcode >= 53000 && zipcode <= 54999) {
      st = 'WI';
      state = 'Wisconsin';
  } else if ( (zipcode >= 82000 && zipcode <= 83199) || zipcode == 83414 ) {
      st = 'WY';
      state = 'Wyoming';
  } else {
      st = 'none';
      state = 'none';
      console.log('No state found matching', zipcode);
  }

  /* Return `state` for full name or `st` for postal abbreviation */
  return st;
}

For the return value:

  • Return state on the last line to get the state's full name; or
  • Return st on the last line to get the state's two-letter postal abbreviation.

Many thanks to @kevin-boucher and @abaldwin99 for help on parsing smaller New England codes and avoiding the dreaded octal evaluation bug with their answers here.

Also thanks for much of the original code goes to this useful page.

Invisible answered 3/3, 2015 at 2:39 Comment(0)
C
6
function getState(zipcode) {
    // Returns false on invalid zip-- else returns {code:"XX" long:"XXXXXXXXX"}

    // Ensure param is a string to prevent unpredictable parsing results
    if (typeof zipcode !== 'string') {
        console.log('Must pass the zipcode as a string. -- Otherwise leading zeros could cause your zip code to be parsed outside base 10.');
        return;
    }

    // Ensure you don't parse codes that start with 0 as octal values
    zipcode = parseInt(zipcode,10);

    // Code blocks alphabetized by state
    var states = [{min: 35000, max:36999, code: 'AL', long: "Alabama"},
    {min: 99500, max:99999, code: 'AK', long: "Alaska"},
    {min: 85000, max:86999, code: 'AZ', long: "Arizona"},
    {min: 71600, max:72999, code: 'AR', long: "Arkansas"},
    {min: 90000, max:96699, code: 'CA', long: "California"},
    {min: 80000, max:81999, code: 'CO', long: "Colorado"},
    {min: 6000,  max:6999,  code: 'CT', long: "Connecticut"},
    {min: 19700, max:19999, code: 'DE', long: "Deleware"},
    {min: 32000, max:34999, code: 'FL', long: "Florida"},
    {min: 30000, max:31999, code: 'GA', long: "Georgia"},
    {min: 96700, max:96999, code: 'HI', long: "Hawaii"},
    {min: 83200, max:83999, code: 'ID', long: "Idaho"},
    {min: 60000, max:62999, code: 'IL', long: "Illinois"},
    {min: 46000, max:47999, code: 'IN', long: "Indiana"},
    {min: 50000, max:52999, code: 'IA', long: "Iowa"},
    {min: 66000, max:67999, code: 'KS', long: "Kansas"},
    {min: 40000, max:42999, code: 'KY', long: "Kentucky"},
    {min: 70000, max:71599, code: 'LA', long: "Louisiana"},
    {min: 3900,  max:4999,  code: 'ME', long: "Maine"},
    {min: 20600, max:21999, code: 'MD', long: "Maryland"},
    {min: 1000,  max:2799,  code: 'MA', long: "Massachusetts"},
    {min: 48000, max:49999, code: 'MI', long: "Michigan"},
    {min: 55000, max:56999, code: 'MN', long: "Minnesota"},
    {min: 38600, max:39999, code: 'MS', long: "Mississippi"},
    {min: 63000, max:65999, code: 'MO', long: "Missouri"},
    {min: 59000, max:59999, code: 'MT', long: "Montana"},
    {min: 27000, max:28999, code: 'NC', long: "North Carolina"},
    {min: 58000, max:58999, code: 'ND', long: "North Dakota"},
    {min: 68000, max:69999, code: 'NE', long: "Nebraska"},
    {min: 88900, max:89999, code: 'NV', long: "Nevada"},
    {min: 3000, max:3899, code: 'NH', long: "New Hampshire"},
    {min: 7000, max:8999, code: 'NJ', long: "New Jersey"},
    {min: 87000, max:88499, code: 'NM', long: "New Mexico"},
    {min: 10000, max:14999, code: 'NY', long: "New York"},
    {min: 43000, max:45999, code: 'OH', long: "Ohio"},
    {min: 73000, max:74999, code: 'OK', long: "Oklahoma"},
    {min: 97000, max:97999, code: 'OR', long: "Oregon"},
    {min: 15000, max:19699, code: 'PA', long: "Pennsylvania"},
    {min: 300, max:999, code: 'PR', long: "Puerto Rico"},
    {min: 2800, max:2999, code: 'RI', long: "Rhode Island"},
    {min: 29000, max:29999, code: 'SC', long: "South Carolina"},
    {min: 57000, max:57999, code: 'SD', long: "South Dakota"},
    {min: 37000, max:38599, code: 'TN', long: "Tennessee"},
    {min: 75000, max:79999, code: 'TX', long: "Texas"},
    {min: 88500, max:88599, code: 'TX', long: "Texas"},
    {min: 84000, max:84999, code: 'UT', long: "Utah"},
    {min: 5000, max:5999, code: 'VT', long: "Vermont"},
    {min: 22000, max:24699, code: 'VA', long: "Virgina"},
    {min: 20000, max:20599, code: 'DC', long: "Washington DC"},
    {min: 98000, max:99499, code: 'WA', long: "Washington"},
    {min: 24700, max:26999, code: 'WV', long: "West Virginia"},
    {min: 53000, max:54999, code: 'WI', long: "Wisconsin"},
    {min: 82000, max:83199, code: 'WY', long: "Wyoming"}];

    var state = states.filter(function(s){
        return s.min <= zipcode && s.max >= zipcode;        
    });

    if (state.length == 0){
        return false;
    } else if (state.length > 1) {
        console.error("Whoops found two states");
    }
    return {code:state[0].code, long:state[0].long};

}
Cords answered 19/3, 2019 at 13:42 Comment(0)
S
5

zippopotam.us has a REST API. Here is an example of how to get a State from a ZIP in pure JavaScript (no libraries):

var getStatebutton = document.getElementById('GetStateButton');

getStatebutton.onclick = function () {
    var zipCode = document.getElementById('ZIPCode');
    var zip = zipCode.value;
    if (!zip) return;
    var url = 'http://api.zippopotam.us/us/' + zip;
    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
            var result = xhr.responseText;
            var zippo = JSON.parse(result);
            var resultDiv = document.getElementById('divResult');
            resultDiv.innerHTML = zippo.places[0].state;
        }
    };
    xhr.open('GET', url, true);
    xhr.send(null);
};
ZIP:
<input type='text' id='ZIPCode' value='90210' />
<button id="GetStateButton">Get State</button>
<p></p>State:
<div id='divResult'></div>

or in jsfiddle if you prefer.

Soporific answered 3/3, 2015 at 11:27 Comment(1)
Good answer, although I'd prefer not to have any external dependencies added to the application. This is lean, but still depends on zippopotam.us.Invisible
T
5

I ended up downloading a CSV file from the United States zip code database. I too did not want to integrate with a dependency API.

The CSV file came with more information than I needed. I parsed the file for only the zip and state columns, and created a Hash of zip code to state code (I'm using Ruby on Rails) that I maintain and use in my application. You could do something similar with Javascript.

There are problems with current revision of the accepted answer of using ranges.

First, this answer does not account for the United States outlying areas like AE (Armed Forces Europe https://www.zip-codes.com/state/ae.asp), AA (Armed Forces Americas https://www.zip-codes.com/state/aa.asp), and others.

Second, ranges change, and zip codes change (though infrequently). The accepted answer has several revisions to add "edge cases" and "corner cases" to the ranges over the years. But I think what's really happening here is that zip codes are changing, and the ranges in that answer are having to constantly keep getting updated. I discovered several zip codes that didn't work for the current revision of the accepted answer. There could be more examples — I didn't do an exhaustive check. [Fiddle]

// Prints DC, but should be MD
// https://www.zip-codes.com/zip-code/20588/zip-code-20588.asp
console.log(`20588: ${getState("20588")}`);

// Prints OK, but should be TX
// https://www.zip-codes.com/zip-code/73960/zip-code-73960.asp
console.log(`73960: ${getState("73960")}`);

// Prints ID, but should be WY
// https://www.zip-codes.com/zip-code/83414/zip-code-83414.asp
console.log(`83414: ${getState("83414")}`);

// Prints OK, but should be TX
// https://www.zip-codes.com/zip-code/73960/zip-code-73960.asp
console.log(`73960: ${getState("73960")}`);

It's true that downloading from the CSV file from the United States zip code database will only represent a point-in-time for zip code data. Anybody going with this solution will need to periodically do a fresh download that file to get the most up-to-date data. But given the guarantees on their website, it's better to rely on this maintained source of truth, rather than relying on ranges.

From their website:

"We constantly monitor USPS data for major changes and release new data when there are significant changes. Our confidence is so high that we offer a money back guarantee: if you find any major changes before we make data updates, we'll issue a full refund."

Trilingual answered 3/11, 2022 at 20:47 Comment(0)
I
3

You can return the most likely state for a given zipcode with an array of ranges.

This is not a validator- not every number in a range is actually assigned as a zip code,and there may be new ranges added in the future.

function stateFromZip(z){
    z= parseInt(z, 10);// removes leading '0'
    if(z<1001 || z>99950) return null;
    var i= 69, next, s, 
    zs= [
        [1001, 2791, 'Massachusetts'], [2801, 2940, 'Rhode Island'], [3031, 3897, 'New Hampshire'], 
        [3901, 4992, 'Maine'], [5001, 5495, 'Vermont'], [5501, 5544, 'Massachusetts'], 
        [5601, 5907, 'Vermont'], [6001, 6389, 'Connecticut'], [6390, 6390, 'New York'], 
        [6401, 6928, 'Connecticut'], [7001, 8989, 'New Jersey'], [10001, 14975, 'New York'], 
        [15001, 19640, 'Pennsylvania'], [19701, 19980, 'Delaware'], [20001, 20039, 'Dist. of Columbia'], 
        [20040, 20167, 'Virginia'], [20042, 20599, 'Dist. of Columbia'], [20331, 20331, 'Maryland'], 
        [20335, 20797, 'Maryland'], [20799, 20799, 'Dist. of Columbia'], [20812, 21930, 'Maryland'], 
        [22001, 24658, 'Virginia'], [24701, 26886, 'West Virginia'], [27006, 28909, 'North Carolina'], 
        [29001, 29948, 'South Carolina'], [30001, 31999, 'Georgia'], [32004, 34997, 'Florida'], 
        [35004, 36925, 'Alabama'], [37010, 38589, 'Tennessee'], [38601, 39776, 'Mississippi'], 
        [39901, 39901, 'Georgia'], [40003, 42788, 'Kentucky'], [43001, 45999, 'Ohio'], 
        [46001, 47997, 'Indiana'], [48001, 49971, 'Michigan'], [50001, 52809, 'Iowa'], 
        [53001, 54990, 'Wisconsin'], [55001, 56763, 'Minnesota'], [57001, 57799, 'South Dakota'], 
        [58001, 58856, 'North Dakota'], [59001, 59937, 'Montana'], [60001, 62999, 'Illinois'], 
        [63001, 65899, 'Missouri'], [66002, 67954, 'Kansas'], [68001, 68118, 'Nebraska'], 
        [68119, 68120, 'Iowa'], [68122, 69367, 'Nebraska'], [70001, 71232, 'Louisiana'], 
        [71233, 71233, 'Mississippi'], [71234, 71497, 'Louisiana'], [73001, 73199, 'Oklahoma'], 
        [73301, 73301, 'Texas'], [73401, 74966, 'Oklahoma'], [75001, 75501, 'Texas'], 
        [75502, 75502, 'Arkansas'], [75503, 79999, 'Texas'], [80001, 81658, 'Colorado'], 
        [82001, 83128, 'Wyoming'], [83201, 83876, 'Idaho'], [84001, 84784, 'Utah'], 
        [85001, 86556, 'Arizona'], [87001, 88441, 'New Mexico'], [88510, 88589, 'Texas'], 
        [88901, 89883, 'Nevada'], [90001, 96162, 'California'], [96701, 96898, 'Hawaii'], 
        [97001, 97920, 'Oregon'], [98001, 99403, 'Washington'], [99501, 99950, 'Alaska']
    ];

    while(i){
        next= zs[--i];
        if(z>next[0] && z<next[1]) return next[2];
    }
    return null;
}

stateFromZip('49125')

/* returned value: (String) Michigan */

Idellaidelle answered 3/3, 2015 at 2:48 Comment(0)
H
1

I realize the OP was originally asking for PHP or JS but I ended up here so someone else might also find this useful.

Here is a C# version with updated zip code ranges (built from a database of 42k zip codes)

public static string GetStateFromZip(int zipCode) {
    return zipCode switch {
        var zip when zip >= 600 && zip <= 999 => "Puerto Rico",
        var zip when zip >= 1001 && zip <= 2791 => "Massachusetts",
        var zip when zip >= 2801 && zip <= 2940 => "Rhode Island",
        var zip when zip >= 3031 && zip <= 3897 => "New Hampshire",
        var zip when zip >= 3901 && zip <= 4992 => "Maine",
        var zip when zip >= 5001 && zip <= 5495 => "Vermont",
        var zip when zip >= 5501 && zip <= 5544 => "Massachusetts",
        var zip when zip >= 5601 && zip <= 5907 => "Vermont",
        var zip when zip >= 6001 && zip <= 6389 => "Connecticut",
        var zip when zip >= 6390 && zip <= 6390 => "New York",
        var zip when zip >= 6401 && zip <= 6928 => "Connecticut",
        var zip when zip >= 7001 && zip <= 8989 => "New Jersey",
        var zip when zip >= 9000 && zip <= 9900 => "Military",
        var zip when zip >= 10001 && zip <= 14975 => "New York",
        var zip when zip >= 15001 && zip <= 19640 => "Pennsylvania",
        var zip when zip >= 19701 && zip <= 19980 => "Delaware",
        var zip when zip >= 20001 && zip <= 20039 => "Washington, D.C.",
        var zip when zip >= 20040 && zip <= 20167 => "Virginia",
        var zip when zip >= 20042 && zip <= 20599 => "Washington, D.C.",
        var zip when zip >= 20331 && zip <= 20331 => "Maryland",
        var zip when zip >= 20335 && zip <= 20797 => "Maryland",
        var zip when zip >= 20799 && zip <= 20799 => "Washington, D.C.",
        var zip when zip >= 20810 && zip <= 21930 => "Maryland",
        var zip when zip >= 22001 && zip <= 24658 => "Virginia",
        var zip when zip >= 24701 && zip <= 26886 => "West Virginia",
        var zip when zip >= 27006 && zip <= 28909 => "North Carolina",
        var zip when zip >= 29001 && zip <= 29948 => "South Carolina",
        var zip when zip >= 30001 && zip <= 31999 => "Georgia",
        var zip when zip >= 32000 && zip <= 34997 => "Florida",
        var zip when zip >= 35004 && zip <= 36925 => "Alabama",
        var zip when zip >= 37010 && zip <= 38589 => "Tennessee",
        var zip when zip >= 38601 && zip <= 39776 => "Mississippi",
        var zip when zip >= 39813 && zip <= 39897 => "Georgia",
        var zip when zip >= 39901 && zip <= 39901 => "Georgia",
        var zip when zip >= 40003 && zip <= 42788 => "Kentucky",
        var zip when zip >= 43001 && zip <= 45999 => "Ohio",
        var zip when zip >= 46001 && zip <= 47997 => "Indiana",
        var zip when zip >= 48001 && zip <= 49971 => "Michigan",
        var zip when zip >= 50001 && zip <= 52809 => "Iowa",
        var zip when zip >= 53001 && zip <= 54990 => "Wisconsin",
        var zip when zip >= 55001 && zip <= 56763 => "Minnesota",
        var zip when zip >= 56901 && zip <= 56999 => "Washington, D.C.",
        var zip when zip >= 57001 && zip <= 57799 => "South Dakota",
        var zip when zip >= 58001 && zip <= 58856 => "North Dakota",
        var zip when zip >= 59001 && zip <= 59937 => "Montana",
        var zip when zip >= 60001 && zip <= 62999 => "Illinois",
        var zip when zip >= 63001 && zip <= 65899 => "Missouri",
        var zip when zip >= 66002 && zip <= 67954 => "Kansas",
        var zip when zip >= 68001 && zip <= 68118 => "Nebraska",
        var zip when zip >= 68119 && zip <= 68120 => "Iowa",
        var zip when zip >= 68122 && zip <= 69367 => "Nebraska",
        var zip when zip >= 70001 && zip <= 71232 => "Louisiana",
        var zip when zip >= 71233 && zip <= 71233 => "Mississippi",
        var zip when zip >= 71234 && zip <= 71497 => "Louisiana",
        var zip when zip >= 71601 && zip <= 72959 => "Arkansas",
        var zip when zip >= 73001 && zip <= 73199 => "Oklahoma",
        var zip when zip >= 73301 && zip <= 73399 => "Texas",
        var zip when zip >= 73401 && zip <= 74966 => "Oklahoma",
        var zip when zip >= 75001 && zip <= 75501 => "Texas",
        var zip when zip >= 75502 && zip <= 75502 => "Arkansas",
        var zip when zip >= 75503 && zip <= 79999 => "Texas",
        var zip when zip >= 80001 && zip <= 81658 => "Colorado",
        var zip when zip >= 82001 && zip <= 83128 => "Wyoming",
        var zip when zip >= 83201 && zip <= 83876 => "Idaho",
        var zip when zip >= 84001 && zip <= 84790 => "Utah",
        var zip when zip >= 85001 && zip <= 86556 => "Arizona",
        var zip when zip >= 87001 && zip <= 88441 => "New Mexico",
        var zip when zip >= 88510 && zip <= 88595 => "Texas",
        var zip when zip >= 88901 && zip <= 89883 => "Nevada",
        var zip when zip >= 90001 && zip <= 96162 => "California",
        var zip when zip >= 96201 && zip <= 96698 => "Military",
        var zip when zip >= 96701 && zip <= 96898 => "Hawaii",
        var zip when zip >= 96910 && zip <= 96932 => "Guam",
        var zip when zip >= 97001 && zip <= 97920 => "Oregon",
        var zip when zip >= 98001 && zip <= 99403 => "Washington",
        var zip when zip >= 99501 && zip <= 99950 => "Alaska",
        _ => ""
    };
}
Hax answered 26/9, 2020 at 18:21 Comment(0)
C
1

Here's the PHP version of @Tony Brasunas accepted answer

<?php

function getState( $zipcode ) {

  /* Ensure we have exactly 5 characters to parse */
  if ( strlen( $zipcode ) !== 5) {
      return 'Must pass a 5-digit $zipcode.';
  }

  $st = '';
  $state = '';

  /* Code cases alphabetized by state */
  if ($zipcode >= 35000 && $zipcode <= 36999) {
      $st = 'AL';
      $state = 'Alabama';
  } else if ($zipcode >= 99500 && $zipcode <= 99999) {
      $st = 'AK';
      $state = 'Alaska';
  } else if ($zipcode >= 85000 && $zipcode <= 86999) {
      $st = 'AZ';
      $state = 'Arizona';
  } else if ($zipcode >= 71600 && $zipcode <= 72999) {
      $st = 'AR';
      $state = 'Arkansas';
  } else if ($zipcode >= 90000 && $zipcode <= 96699) {
      $st = 'CA';
      $state = 'California';
  } else if ($zipcode >= 80000 && $zipcode <= 81999) {
      $st = 'CO';
      $state = 'Colorado';
  } else if (($zipcode >= 6000 && $zipcode <= 6389) || ($zipcode >= 6391 && $zipcode <= 6999)) {
      $st = 'CT';
      $state = 'Connecticut';
  } else if ($zipcode >= 19700 && $zipcode <= 19999) {
      $st = 'DE';
      $state = 'Delaware';
  } else if ($zipcode >= 32000 && $zipcode <= 34999) {
      $st = 'FL';
      $state = 'Florida';
  } else if ( ($zipcode >= 30000 && $zipcode <= 31999) || ($zipcode >= 39800 && $zipcode <= 39999) ) {
      $st = 'GA';
      $state = 'Georgia';
  } else if ($zipcode >= 96700 && $zipcode <= 96999) {
      $st = 'HI';
      $state = 'Hawaii';
  } else if ($zipcode >= 83200 && $zipcode <= 83999) {
      $st = 'ID';
      $state = 'Idaho';
  } else if ($zipcode >= 60000 && $zipcode <= 62999) {
      $st = 'IL';
      $state = 'Illinois';
  } else if ($zipcode >= 46000 && $zipcode <= 47999) {
      $st = 'IN';
      $state = 'Indiana';
  } else if ($zipcode >= 50000 && $zipcode <= 52999) {
      $st = 'IA';
      $state = 'Iowa';
  } else if ($zipcode >= 66000 && $zipcode <= 67999) {
      $st = 'KS';
      $state = 'Kansas';
  } else if ($zipcode >= 40000 && $zipcode <= 42999) {
      $st = 'KY';
      $state = 'Kentucky';
  } else if ($zipcode >= 70000 && $zipcode <= 71599) {
      $st = 'LA';
      $state = 'Louisiana';
  } else if ($zipcode >= 3900 && $zipcode <= 4999) {
      $st = 'ME';
      $state = 'Maine';
  } else if ($zipcode >= 20600 && $zipcode <= 21999) {
      $st = 'MD';
      $state = 'Maryland';
  } else if ( ($zipcode >= 1000 && $zipcode <= 2799) || ($zipcode == 5501) || ($zipcode == 5544 ) ) {
      $st = 'MA';
      $state = 'Massachusetts';
  } else if ($zipcode >= 48000 && $zipcode <= 49999) {
      $st = 'MI';
      $state = 'Michigan';
  } else if ($zipcode >= 55000 && $zipcode <= 56899) {
      $st = 'MN';
      $state = 'Minnesota';
  } else if ($zipcode >= 38600 && $zipcode <= 39999) {
      $st = 'MS';
      $state = 'Mississippi';
  } else if ($zipcode >= 63000 && $zipcode <= 65999) {
      $st = 'MO';
      $state = 'Missouri';
  } else if ($zipcode >= 59000 && $zipcode <= 59999) {
      $st = 'MT';
      $state = 'Montana';
  } else if ($zipcode >= 27000 && $zipcode <= 28999) {
      $st = 'NC';
      $state = 'North Carolina';
  } else if ($zipcode >= 58000 && $zipcode <= 58999) {
      $st = 'ND';
      $state = 'North Dakota';
  } else if ($zipcode >= 68000 && $zipcode <= 69999) {
      $st = 'NE';
      $state = 'Nebraska';
  } else if ($zipcode >= 88900 && $zipcode <= 89999) {
      $st = 'NV';
      $state = 'Nevada';
  } else if ($zipcode >= 3000 && $zipcode <= 3899) {
      $st = 'NH';
      $state = 'New Hampshire';
  } else if ($zipcode >= 7000 && $zipcode <= 8999) {
      $st = 'NJ';
      $state = 'New Jersey';
  } else if ($zipcode >= 87000 && $zipcode <= 88499) {
      $st = 'NM';
      $state = 'New Mexico';
  } else if ( ($zipcode >= 10000 && $zipcode <= 14999) || ($zipcode == 6390) || ($zipcode == 501) || ($zipcode == 544) ) {
      $st = 'NY';
      $state = 'New York';
  } else if ($zipcode >= 43000 && $zipcode <= 45999) {
      $st = 'OH';
      $state = 'Ohio';
  } else if (($zipcode >= 73000 && $zipcode <= 73199) || ($zipcode >= 73400 && $zipcode <= 74999) ) {
      $st = 'OK';
      $state = 'Oklahoma';
  } else if ($zipcode >= 97000 && $zipcode <= 97999) {
      $st = 'OR';
      $state = 'Oregon';
  } else if ($zipcode >= 15000 && $zipcode <= 19699) {
      $st = 'PA';
      $state = 'Pennsylvania';
  } else if ($zipcode >= 300 && $zipcode <= 999) {
      $st = 'PR';
      $state = 'Puerto Rico';
  } else if ($zipcode >= 2800 && $zipcode <= 2999) {
      $st = 'RI';
      $state = 'Rhode Island';
  } else if ($zipcode >= 29000 && $zipcode <= 29999) {
      $st = 'SC';
      $state = 'South Carolina';
  } else if ($zipcode >= 57000 && $zipcode <= 57999) {
      $st = 'SD';
      $state = 'South Dakota';
  } else if ($zipcode >= 37000 && $zipcode <= 38599) {
      $st = 'TN';
      $state = 'Tennessee';
  } else if ( ($zipcode >= 75000 && $zipcode <= 79999) || ($zipcode >= 73301 && $zipcode <= 73399) ||  ($zipcode >= 88500 && $zipcode <= 88599) ) {
      $st = 'TX';
      $state = 'Texas';
  } else if ($zipcode >= 84000 && $zipcode <= 84999) {
      $st = 'UT';
      $state = 'Utah';
  } else if ($zipcode >= 5000 && $zipcode <= 5999) {
      $st = 'VT';
      $state = 'Vermont';
  } else if ( ($zipcode >= 20100 && $zipcode <= 20199) || ($zipcode >= 22000 && $zipcode <= 24699) || ($zipcode == 20598) ) {
      $st = 'VA';
      $state = 'Virgina';
  } else if ( ($zipcode >= 20000 && $zipcode <= 20099) || ($zipcode >= 20200 && $zipcode <= 20599) || ($zipcode >= 56900 && $zipcode <= 56999) ) {
      $st = 'DC';
      $state = 'Washington DC';
  } else if ($zipcode >= 98000 && $zipcode <= 99499) {
      $st = 'WA';
      $state = 'Washington';
  } else if ($zipcode >= 24700 && $zipcode <= 26999) {
      $st = 'WV';
      $state = 'West Virginia';
  } else if ($zipcode >= 53000 && $zipcode <= 54999) {
      $st = 'WI';
      $state = 'Wisconsin';
  } else if ($zipcode >= 82000 && $zipcode <= 83199) {
      $st = 'WY';
      $state = 'Wyoming';
  } else {
      $st = 'none';
      $state = 'none';
      return 'No state found matching' . $zipcode;
  }

  return $st;
}
Clow answered 6/10, 2021 at 19:41 Comment(0)
L
1

Be sure to consider your specific needs before using unmodified versions of the supplied answers.

For example, if you need "extended" locations such as the following you may want to adjust the provided code to include areas such as:

Here are some (rough) adjustments that could be added to the currently accepted Tony Brasunas answer:

  } else if (zipcode >= 601 && zipcode <= 795) || (zipcode >= 901 && zipcode <= 988) { // Using (zipcode >= 300 && zipcode <= 999) includes VI
      st = 'PR';
      state = 'Puerto Rico'; // https://www.zip-codes.com/state/pr.asp
 } else if (zipcode >= 801 && zipcode <= 851) {
      st = 'VI';
      state = 'Virgin Islands'; // https://www.zip-codes.com/state/vi.asp
 } else if (zipcode >= 9001 && zipcode <= 9977) {
      st = 'AE';
      state = 'Armed Forces Europe'; // https://www.zip-codes.com/state/ae.asp
 } else if (zipcode >= 34001 && zipcode <= 34095) {
      st = 'AA';
      state = 'Armed Forces Americas'; // https://www.zip-codes.com/state/aa.asp
 }
Larrikin answered 30/12, 2021 at 18:10 Comment(0)
O
1

Here is JS version tested on a real US zipcode database with 42735 zip codes. Includes exceptions and other US territories like American Samoa, District of Columbia, Guam, Northern Mariana Islands, Puerto Rico, United States Virgin Islands

export const STATE_ZIPCODE = [
  {
    stateCode: "VI",
    stateName: "Virgin Islands",
    zipcodeRange: ["00801", "00851"],
  },
  {
    stateCode: "PR",
    stateName: "Puerto Rico",
    zipcodeRange: ["00601", "00988"],
  },
  {
    stateCode: "RI",
    stateName: "Rhode Island",
    zipcodeRange: ["02801", "02940"],
  },
  {
    stateCode: "NH",
    stateName: "New Hampshire",
    zipcodeRange: ["03031", "03897"],
  },
  {
    stateCode: "GU",
    stateName: "Guam",
    zipcodeRange: ["96910", "96932"],
  },
  {
    stateCode: "MP",
    stateName: "Mariana Islands",
    zipcodeRange: ["96950", "96952"],
  },

  {
    stateCode: "AP",
    stateName: "Armed Forces Pacific",
    zipcodeRange: ["96200", "96600"],
  },

  // Dist of Columbia
  {
    stateCode: "DC",
    stateName: "Dist of Columbia",
    zipcodeRange: ["20001", "20039"],
  },
  {
    stateCode: "DC",
    stateName: "Dist of Columbia",
    zipcodeRange: ["20040", "20100"], 
  },
  {
    stateCode: "DC",
    stateName: "Dist of Columbia",
    zipcodeRange: ["20199", "20599"], 
  },
  {
    stateCode: "DC",
    stateName: "Dist of Columbia",
    zipcodeRange: ["20799", "20799"],
  },
  {
    stateCode: "DC",
    stateName: "Dist of Columbia",
    zipcodeRange: ["56901", "56999"],
  },
  // ---------------
  {
    stateCode: "AL",
    stateName: "Alabama",
    zipcodeRange: ["35004", "36925"],
  },
  {
    stateCode: "AK",
    stateName: "Alaska",
    zipcodeRange: ["99501", "99950"],
  },
  {
    stateCode: "AZ",
    stateName: "Arizona",
    zipcodeRange: ["85001", "86556"],
  },
  {
    stateCode: "AR",
    stateName: "Arkansas",
    zipcodeRange: ["71601", "72959"],
  },
  {
    stateCode: "CA",
    stateName: "California",
    zipcodeRange: ["90001", "96162"],
  },
  {
    stateCode: "CO",
    stateName: "Colorado",
    zipcodeRange: ["80001", "81658"],
  },
  {
    stateCode: "CT",
    stateName: "Connecticut",
    zipcodeRange: ["06001", "06928"],
  },
  {
    stateCode: "DE",
    stateName: "Delaware",
    zipcodeRange: ["19701", "19980"],
  },
  {
    stateCode: "FL",
    stateName: "Florida",
    zipcodeRange: ["32003", "34997"],
  },
  {
    stateCode: "TN",
    stateName: "Tennessee",
    zipcodeRange: ["37010", "38589"],
  },
  {
    stateCode: "MS",
    stateName: "Mississippi",
    zipcodeRange: ["38601", "39776"],
  },
  {
    stateCode: "GA",
    stateName: "Georgia",
    zipcodeRange: ["30002", "39901"],
  },
  {
    stateCode: "HI",
    stateName: "Hawaii",
    zipcodeRange: ["96701", "96898"],
  },
  {
    stateCode: "ID",
    stateName: "Idaho",
    zipcodeRange: ["83201", "83877"],
  },
  {
    stateCode: "IL",
    stateName: "Illinois",
    zipcodeRange: ["60001", "62999"],
  },
  {
    stateCode: "IN",
    stateName: "Indiana",
    zipcodeRange: ["46001", "47997"],
  },
  {
    stateCode: "IA",
    stateName: "Iowa",
    zipcodeRange: ["50001", "52809"],
  },
  {
    stateCode: "KS",
    stateName: "Kansas",
    zipcodeRange: ["66002", "67954"],
  },
  {
    stateCode: "KY",
    stateName: "Kentucky",
    zipcodeRange: ["40003", "42788"],
  },
  {
    stateCode: "LA",
    stateName: "Louisiana",
    zipcodeRange: ["70001", "71497"],
  },
  {
    stateCode: "ME",
    stateName: "Maine",
    zipcodeRange: ["03901", "04992"],
  },
  {
    stateCode: "MD",
    stateName: "Maryland",
    zipcodeRange: ["20588", "21930"],
  },
  {
    stateCode: "VT",
    stateName: "Vermont",
    zipcodeRange: ["05001", "05907"],
  },
  {
    stateCode: "MA",
    stateName: "Massachusetts",
    zipcodeRange: ["01001", "05544"],
  },
  {
    stateCode: "MI",
    stateName: "Michigan",
    zipcodeRange: ["48001", "49971"],
  },
  {
    stateCode: "MN",
    stateName: "Minnesota",
    zipcodeRange: ["55001", "56763"],
  },
  {
    stateCode: "MO",
    stateName: "Missouri",
    zipcodeRange: ["63001", "65899"],
  },
  {
    stateCode: "MT",
    stateName: "Montana",
    zipcodeRange: ["59001", "59937"],
  },
  {
    stateCode: "NE",
    stateName: "Nebraska",
    zipcodeRange: ["68001", "69367"],
  },
  {
    stateCode: "NV",
    stateName: "Nevada",
    zipcodeRange: ["88901", "89883"],
  },
  {
    stateCode: "NJ",
    stateName: "New Jersey",
    zipcodeRange: ["07001", "08989"],
  },
  {
    stateCode: "NM",
    stateName: "New Mexico",
    zipcodeRange: ["87001", "88439"],
  },
  {
    stateCode: "NY",
    stateName: "New York",
    zipcodeRange: ["00501", "14925"],
  },
  {
    stateCode: "NC",
    stateName: "North Carolina",
    zipcodeRange: ["27006", "28909"],
  },
  {
    stateCode: "ND",
    stateName: "North Dakota",
    zipcodeRange: ["58001", "58856"],
  },
  {
    stateCode: "OH",
    stateName: "Ohio",
    zipcodeRange: ["43001", "45999"],
  },
  {
    stateCode: "OK",
    stateName: "Oklahoma",
    zipcodeRange: ["73001", "74966"],
  },
  {
    stateCode: "OR",
    stateName: "Oregon",
    zipcodeRange: ["97001", "97920"],
  },
  {
    stateCode: "PA",
    stateName: "Pennsylvania",
    zipcodeRange: ["15001", "19640"],
  },
  {
    stateCode: "SC",
    stateName: "South Carolina",
    zipcodeRange: ["29001", "29945"],
  },
  {
    stateCode: "SD",
    stateName: "South Dakota",
    zipcodeRange: ["57001", "57799"],
  },
  {
    stateCode: "UT",
    stateName: "Utah",
    zipcodeRange: ["84001", "84791"],
  },
  {
    stateCode: "VA",
    stateName: "Virginia",
    zipcodeRange: ["20040", "24658"],
  },
  {
    stateCode: "WA",
    stateName: "Washington",
    zipcodeRange: ["98001", "99403"],
  },
  {
    stateCode: "WV",
    stateName: "West Virginia",
    zipcodeRange: ["24701", "26886"],
  },
  {
    stateCode: "WI",
    stateName: "Wisconsin",
    zipcodeRange: ["53001", "54990"],
  },
  {
    stateCode: "WY",
    stateName: "Wyoming",
    zipcodeRange: ["82001", "83414"],
  },
  {
    stateCode: "TX",
    stateName: "Texas",
    zipcodeRange: ["73301", "88595"],
  },
];

export function getStateCodeByZipcode(zipcode) {
  const EXCEPTION_ZIPCODES = {
    "05501": "MA",
    "05544": "MA",
    "06390": "NY",
    "20199": "VA",
    "20799": "MD",
    "73301": "TX",
    "73344": "TX",
    "83888": "ID",
    "88441": "NM",
    "72643": "MO",
    "73960": "TX",
    "83414": "WY",
    "20588": "MD",
    "20598": "VA",
    "88888": "DC",
  };

  if (EXCEPTION_ZIPCODES[zipcode]) return EXCEPTION_ZIPCODES[zipcode]

  if (zipcode.startsWith("09")) return "AE";
  if (zipcode.startsWith("340")) return "AA";
  if (zipcode.startsWith("96799")) return "AS";
  if (["96960", "96970"].some((prefix) => zipcode.startsWith(prefix))) return "MH";
  if (["96939", "96940"].some((prefix) => zipcode.startsWith(prefix))) return "PW";
  if (["962", "963", "964", "965", "966"].some((prefix) => zipcode.startsWith(prefix))) return "AP";
  if (["96941", "96942", "96943", "96944"].some((prefix) => zipcode.startsWith(prefix)))return "FM";

  const stateInfo = STATE_ZIPCODE.find((stateInfo) => {
    const zipcodes = [...stateInfo.zipcodeRange, zipcode].sort();

    return zipcodes[1] === zipcode;
  });

  return stateInfo ? stateInfo.stateCode : null;
}
Orangeade answered 18/8, 2023 at 21:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.