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

三世轮回 提交于 2019-12-04 13:49:25

问题


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.


回答1:


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

Here's a lightweight JS solution that determines the state and returns it as its two-letter postal abbreviation. You can return the state's full name instead by returning state instead of st on the last line.

function getState(zipcode) {

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

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

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

    const st;
    const state;

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

    return st;
}

Many thanks to the help from @kevin-boucher and @abaldwin99 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.

Many thanks to the help from @kevin-boucher and @abaldwin99 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.




回答2:


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 */




回答3:


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.




回答4:


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};

}


来源:https://stackoverflow.com/questions/28821804/how-can-i-quickly-determine-the-state-for-a-given-zipcode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!