How can I quickly determine the State for a given

2019-04-03 06:22发布

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.

4条回答
走好不送
2楼-- · 2019-04-03 06:28

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-03 06:43
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};

}
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-04-03 06:47

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:

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 you don't parse codes that start with 0 as octal values
    const thiszip = parseInt(zipcode,10); 

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

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.

查看更多
我只想做你的唯一
5楼-- · 2019-04-03 06:48

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

查看更多
登录 后发表回答