JavaScript zip code to state conversion

I think it makes very little sense for a website registration to ask you for your zipcode AND your state. That’s redundant; your state can be inferred from your zipcode. Studies reveal that fewer fields in a form sharply increases conversion rate.

function zipToState(zip) {
    var state;
	if (zip >= '99501' && zip <= '99950')  { state = 'AK'; }
	else if (zip >= '35004' && zip <= '36925')  { state = 'AL'; }
	else if (zip >= '71601' && zip <= '72959')  { state = 'AR'; }
	else if (zip >= '75502' && zip <= '75502')  { state = 'AR'; }
	else if (zip >= '85001' && zip <= '86556')  { state = 'AZ'; }
	else if (zip >= '90001' && zip <= '96162')  { state = 'CA'; }
	else if (zip >= '80001' && zip <= '81658')  { state = 'CO'; }
	else if (zip >= '06001' && zip <= '06389')  { state = 'CT'; }
	else if (zip >= '06401' && zip <= '06928')  { state = 'CT'; }
	else if (zip >= '20001' && zip <= '20039')  { state = 'DC'; }
	else if (zip >= '20042' && zip <= '20599')  { state = 'DC'; }
	else if (zip >= '20799' && zip <= '20799')  { state = 'DC'; }
	else if (zip >= '19701' && zip <= '19980')  { state = 'DE'; }
	else if (zip >= '32004' && zip <= '34997')  { state = 'FL'; }
	else if (zip >= '30001' && zip <= '31999')  { state = 'GA'; }
	else if (zip >= '39901' && zip <= '39901')  { state = 'GA'; }
	else if (zip >= '96701' && zip <= '96898')  { state = 'HI'; }
	else if (zip >= '50001' && zip <= '52809')  { state = 'IA'; }
	else if (zip >= '68119' && zip <= '68120')  { state = 'IA'; }
	else if (zip >= '83201' && zip <= '83876')  { state = 'ID'; }
	else if (zip >= '60001' && zip <= '62999')  { state = 'IL'; }
	else if (zip >= '46001' && zip <= '47997')  { state = 'IN'; }
	else if (zip >= '66002' && zip <= '67954')  { state = 'KS'; }
	else if (zip >= '40003' && zip <= '42788')  { state = 'KY'; }
	else if (zip >= '70001' && zip <= '71232')  { state = 'LA'; }
	else if (zip >= '71234' && zip <= '71497')  { state = 'LA'; }
	else if (zip >= '01001' && zip <= '02791')  { state = 'MA'; }
	else if (zip >= '05501' && zip <= '05544')  { state = 'MA'; }
	else if (zip >= '20331' && zip <= '20331')  { state = 'MD'; }
	else if (zip >= '20335' && zip <= '20797')  { state = 'MD'; }
	else if (zip >= '20812' && zip <= '21930')  { state = 'MD'; }
	else if (zip >= '03901' && zip <= '04992')  { state = 'ME'; }
	else if (zip >= '48001' && zip <= '49971')  { state = 'MI'; }
	else if (zip >= '55001' && zip <= '56763')  { state = 'MN'; }
	else if (zip >= '63001' && zip <= '65899')  { state = 'MO'; }
	else if (zip >= '38601' && zip <= '39776')  { state = 'MS'; }
	else if (zip >= '71233' && zip <= '71233')  { state = 'MS'; }
	else if (zip >= '59001' && zip <= '59937')  { state = 'MT'; }
	else if (zip >= '27006' && zip <= '28909')  { state = 'NC'; }
	else if (zip >= '58001' && zip <= '58856')  { state = 'ND'; }
	else if (zip >= '68001' && zip <= '68118')  { state = 'NE'; }
	else if (zip >= '68122' && zip <= '69367')  { state = 'NE'; }
	else if (zip >= '03031' && zip <= '03897')  { state = 'NH'; }
	else if (zip >= '07001' && zip <= '08989')  { state = 'NJ'; }
	else if (zip >= '87001' && zip <= '88441')  { state = 'NM'; }
	else if (zip >= '88901' && zip <= '89883')  { state = 'NV'; }
	else if (zip >= '06390' && zip <= '06390')  { state = 'NY'; }
	else if (zip >= '10001' && zip <= '14975')  { state = 'NY'; }
	else if (zip >= '43001' && zip <= '45999')  { state = 'OH'; }
	else if (zip >= '73001' && zip <= '73199')  { state = 'OK'; }
	else if (zip >= '73401' && zip <= '74966')  { state = 'OK'; }
	else if (zip >= '97001' && zip <= '97920')  { state = 'OR'; }
	else if (zip >= '15001' && zip <= '19640')  { state = 'PA'; }
	else if (zip >= '02801' && zip <= '02940')  { state = 'RI'; }
	else if (zip >= '29001' && zip <= '29948')  { state = 'SC'; }
	else if (zip >= '57001' && zip <= '57799')  { state = 'SD'; }
	else if (zip >= '37010' && zip <= '38589')  { state = 'TN'; }
	else if (zip >= '73301' && zip <= '73301')  { state = 'TX'; }
	else if (zip >= '75001' && zip <= '75501')  { state = 'TX'; }
	else if (zip >= '75503' && zip <= '79999')  { state = 'TX'; }
	else if (zip >= '88510' && zip <= '88589')  { state = 'TX'; }
	else if (zip >= '84001' && zip <= '84784')  { state = 'UT'; }
	else if (zip >= '20040' && zip <= '20041')  { state = 'VA'; }
	else if (zip >= '20040' && zip <= '20167')  { state = 'VA'; }
	else if (zip >= '20042' && zip <= '20042')  { state = 'VA'; }
	else if (zip >= '22001' && zip <= '24658')  { state = 'VA'; }
	else if (zip >= '05001' && zip <= '05495')  { state = 'VT'; }
	else if (zip >= '05601' && zip <= '05907')  { state = 'VT'; }
	else if (zip >= '98001' && zip <= '99403')  { state = 'WA'; }
	else if (zip >= '53001' && zip <= '54990')  { state = 'WI'; }
	else if (zip >= '24701' && zip <= '26886')  { state = 'WV'; }
	else if (zip >= '82001' && zip <= '83128')  { state = 'WY'; }
	return state;
}

Thanks to http://www.novicksoftware.com/udfOfWeek/Vol2/T-SQL-UDF-Vol-2-Num-48-udf_Addr_Zip5ToST.htm for the data.



Subscribe to the blog via email.

AwesomenessReminders: a great and hilarious way to show someone in your life that you care.
AwesomenessReminders is owned and operated by me, Zachary Burt.



  • http://www.stevepavlina.com/forums/business-financial/43462-anyone-running-website-user-registration.html#post522073 For anyone running a website with user registration…

    [...] [...]

  • http://matt.might.net/ Matt Might

    You should have a “var state” at the top of the function, so you don’t clobber a global variable named “state”.

  • http://matt.might.net/ Matt Might

    You should have a “var state” at the top of the function, so you don't clobber a global variable named “state”.

  • http://venodesigns.net/ Ryan McGrath

    Y’know, in rare cases, a switch statement isn’t a bad thing to use. ;P

    Function itself is fairly useful, though, I’ll give you that. Thanks.

  • ryanmcgrath

    Y'know, in rare cases, a switch statement isn't a bad thing to use. ;P

    Function itself is fairly useful, though, I'll give you that. Thanks.

  • http://www.zacharyburt.com/ Zachary Burt

    How would you use a switch here? I didn’t know you could use a switch for a range.

  • zburt

    Please explain how you would use a switch here. I did not know that you could use a switch for a range.

  • http://www.zacharyburt.com/ Zachary Burt

    Fixed, good call.

  • zburt

    Fixed, good call.

  • http://ixmat.us/ Parnell Springmeyer

    This is only useful for USA postal codes, making it international could be tricky – hence why many websites still ask for your “State or Province”…

    Nice idea though and would work well for web applications that reject registrants from other countries…

  • http://ixmat.us/ Parnell Springmeyer

    This is only useful for USA postal codes, making it international could be tricky – hence why many websites still ask for your “State or Province”…

    Nice idea though and would work well for web applications that reject registrants from other countries…

  • jonathan

    Your function doesn’t work with APO/FPO addresses.

  • jonathan

    Your function doesn't work with APO/FPO addresses.

  • axod

    // Slightly more sane (IMHO)

    function zipToState(zip) {
    var states = ['99501 99950 AK','35004 36925 AL','71601 72959 AR','75502 75502 AR'
    ,'85001 86556 AZ','90001 96162 CA','80001 81658 CO','06001 06389 CT'
    ,'06401 06928 CT','20001 20039 DC','20042 20599 DC','20799 20799 DC'
    ,'19701 19980 DE','32004 34997 FL','30001 31999 GA','39901 39901 GA'
    ,'96701 96898 HI','50001 52809 IA','68119 68120 IA','83201 83876 ID'
    ,'60001 62999 IL','46001 47997 IN','66002 67954 KS','40003 42788 KY'
    ,'70001 71232 LA','71234 71497 LA','01001 02791 MA','05501 05544 MA'
    ,'20331 20331 MD','20335 20797 MD','20812 21930 MD','03901 04992 ME'
    ,'48001 49971 MI','55001 56763 MN','63001 65899 MO','38601 39776 MS'
    ,'71233 71233 MS','59001 59937 MT','27006 28909 NC','58001 58856 ND'
    ,'68001 68118 NE','68122 69367 NE','03031 03897 NH','07001 08989 NJ'
    ,'87001 88441 NM','88901 89883 NV','06390 06390 NY','10001 14975 NY'
    ,'43001 45999 OH','73001 73199 OK','73401 74966 OK','97001 97920 OR'
    ,'15001 19640 PA','02801 02940 RI','29001 29948 SC','57001 57799 SD'
    ,'37010 38589 TN','73301 73301 TX','75001 75501 TX','75503 79999 TX'
    ,'88510 88589 TX','84001 84784 UT','20040 20041 VA','20040 20167 VA'
    ,'20042 20042 VA','22001 24658 VA','05001 05495 VT','05601 05907 VT'
    ,'98001 99403 WA','53001 54990 WI','24701 26886 WV','82001 83128 WY'];
    for(var i=0;i=d[0] && zip<=d[1]) return d[2];
    }
    }

  • axod

    // Slightly more sane (IMHO)

    function zipToState(zip) {
    var states = ['99501 99950 AK','35004 36925 AL','71601 72959 AR','75502 75502 AR'
    ,'85001 86556 AZ','90001 96162 CA','80001 81658 CO','06001 06389 CT'
    ,'06401 06928 CT','20001 20039 DC','20042 20599 DC','20799 20799 DC'
    ,'19701 19980 DE','32004 34997 FL','30001 31999 GA','39901 39901 GA'
    ,'96701 96898 HI','50001 52809 IA','68119 68120 IA','83201 83876 ID'
    ,'60001 62999 IL','46001 47997 IN','66002 67954 KS','40003 42788 KY'
    ,'70001 71232 LA','71234 71497 LA','01001 02791 MA','05501 05544 MA'
    ,'20331 20331 MD','20335 20797 MD','20812 21930 MD','03901 04992 ME'
    ,'48001 49971 MI','55001 56763 MN','63001 65899 MO','38601 39776 MS'
    ,'71233 71233 MS','59001 59937 MT','27006 28909 NC','58001 58856 ND'
    ,'68001 68118 NE','68122 69367 NE','03031 03897 NH','07001 08989 NJ'
    ,'87001 88441 NM','88901 89883 NV','06390 06390 NY','10001 14975 NY'
    ,'43001 45999 OH','73001 73199 OK','73401 74966 OK','97001 97920 OR'
    ,'15001 19640 PA','02801 02940 RI','29001 29948 SC','57001 57799 SD'
    ,'37010 38589 TN','73301 73301 TX','75001 75501 TX','75503 79999 TX'
    ,'88510 88589 TX','84001 84784 UT','20040 20041 VA','20040 20167 VA'
    ,'20042 20042 VA','22001 24658 VA','05001 05495 VT','05601 05907 VT'
    ,'98001 99403 WA','53001 54990 WI','24701 26886 WV','82001 83128 WY'];
    for(var i=0;i<states.length;i++) {
    var d = states[i].split(' ');
    if (zip>=d[0] && zip<=d[1]) return d[2];
    }
    }

  • jonathan

    Sorry I was so terse earlier; I was holding the baby and could only type with one hand. One thing that’s nice about being able to select your state (or enter it, rather than select from a dropdown) is that it means I don’t have to rely on you to be right.

    As long as you use this sort of code to ease the form process, rather than asserting that it knows better than me, but when I can’t enter the actual state I live in because your code says I live in another state, what’s that going to do for your conversion rate?

  • jonathan

    Sorry I was so terse earlier; I was holding the baby and could only type with one hand. One thing that's nice about being able to select your state (or enter it, rather than select from a dropdown) is that it means I don't have to rely on you to be right.

    As long as you use this sort of code to ease the form process, rather than asserting that it knows better than me, but when I can't enter the actual state I live in because your code says I live in another state, what's that going to do for your conversion rate?

  • Anonymous

    You can also use the census data to convert zip to city and state, eliminating one more field.

  • toddiceton

    You can also use the census data to convert zip to city and state, eliminating one more field.

  • http://learnyouanrubyforawesomepower.info/ Kurtis Rainbolt-Greene

    US Zip codes are related to states only by the first digit.

    Here’s my ruby example:

    def state?(zip)
    case zip[0]
    when 0 then states = ['CT','MA','ME','NH','NJ','PR','RI','VT','VI','AE']
    when 1 then states = ['DE','NY','PA']
    when 2 then states = ['DC','MD','NC','SC','VA','WV']
    when 3 then states = ['AL','FL','GA','MS','TN','AA']
    when 4 then states = ['IN','KY','MI','OH']
    when 5 then states = ['IA','HN','MT','ND','SD','WI']
    when 6 then states = ['IL','KS','MO','NE']
    when 7 then states = ['AR','LA','OK','TX']
    when 8 then states = ['AZ','CO','ID','NM','NV','UT','WY']
    when 9 then states = ['AK','AS','CA','GU','HI','MH','FM','MP','OR','PW','WA','AP']
    else return null
    end
    return states
    end

  • http://learnyouanrubyforawesomepower.info/ Kurtis Rainbolt-Greene

    US Zip codes are related to states only by the first digit.

    Here's my ruby example:

    def state?(zip)
    case zip[0]
    when 0 then states = ['CT','MA','ME','NH','NJ','PR','RI','VT','VI','AE']
    when 1 then states = ['DE','NY','PA']
    when 2 then states = ['DC','MD','NC','SC','VA','WV']
    when 3 then states = ['AL','FL','GA','MS','TN','AA']
    when 4 then states = ['IN','KY','MI','OH']
    when 5 then states = ['IA','HN','MT','ND','SD','WI']
    when 6 then states = ['IL','KS','MO','NE']
    when 7 then states = ['AR','LA','OK','TX']
    when 8 then states = ['AZ','CO','ID','NM','NV','UT','WY']
    when 9 then states = ['AK','AS','CA','GU','HI','MH','FM','MP','OR','PW','WA','AP']
    else return null
    end
    return states
    end

  • paul

    Hi,
    There’s an error in the code, and its in the origina linked SQL too
    13 else if (zip >= ’20042′ && zip = ’20040′ && zip <= '20167') { state = 'VA'; }
    both of these conditions could be true, I've no idea whether zipcodes between 20042 and 20167 should be in DC or VA.

  • paul

    Hi,
    There's an error in the code, and its in the origina linked SQL too
    13 else if (zip >= '20042' && zip <= '20599') { state = 'DC'; }
    66 else if (zip >= '20040' && zip <= '20167') { state = 'VA'; }
    both of these conditions could be true, I've no idea whether zipcodes between 20042 and 20167 should be in DC or VA.

  • http://www.netcrema.com/?p=29041 JavaScript zip code to state conversion « Zachary Burt’s Blog « Netcrema – creme de la social news via digg + delicious + stumpleupon + reddit

    [...] JavaScript zip code to state conversion « Zachary Burt’s Blogzacharyburt.com [...]

  • http://learnyouanrubyforawesomepower.info/ Kurtis Rainbolt-Greene

    On further review I’ve found this: http://www.census.gov/tiger/tms/gazetteer/zips.txt

    so:

    def state?(zip)
    zipcodelist = File.open(‘zips.txt’)
    suggestedlist = zipcodelist.grep(/(.*”#{zip}d+”.*)/)
    end

  • http://learnyouanrubyforawesomepower.info/ Kurtis Rainbolt-Greene

    On further review I've found this: http://www.census.gov/tiger/tms/gazetteer/zips.txt

    so:

    def state?(zip)
    zipcodelist = File.open('zips.txt')
    suggestedlist = zipcodelist.grep(/(.*”#{zip}d+”.*)/)
    end

  • http://www.drakz.com/online/code/?p=5391 JavaScript zip code to state conversion « Zachary Burt's Blog | Drakz Free Online Service

    [...] original post here: JavaScript zip code to state conversion « Zachary Burt's Blog Share and [...]

  • http://www.bar-code-label-livetoday.co.cc/javascript-zip-code-to-state-conversion-%c2%ab-zachary-burts-blog-2/ JavaScript zip code to state conversion « Zachary Burt's Blog

    [...] the original post: JavaScript zip code to state conversion « Zachary Burt's Blog Posted in Code | Tags: a-website-registration, ask-you, Code, for-your, inferred-from, [...]

  • http://shadow14l.mp/ Shadow14l

    A range is an array like items so there are no ranges in here. If you mean “zip >= ’99501′ && zip = means greater than or equal to and <= means less than or equal to. Normally in most languages the zip variable would have to have a value of an integer, but javascript's variables are dynamic.

  • http://shadow14l.mp/ Shadow14l

    A range is an array like items so there are no ranges in here. If you mean “zip >= '99501' && zip <= '99950'”, than that is not a range, it is two conditions linked with an “and” conditional. >= means greater than or equal to and <= means less than or equal to. Normally in most languages the zip variable would have to have a value of an integer, but javascript's variables are dynamic.

  • http://learnyouanrubyforawesomepower.info/ Kurtis Rainbolt-Greene

    On further review I've found this: http://www.census.gov/tiger/tms/gazetteer/zips.txt

    so:

    def state?(zip)
    zipcodelist = File.open('zips.txt')
    suggestedlist = zipcodelist.grep(/(.*”#{zip}d+”.*)/)
    end

  • http://shadow14l.mp/ Shadow14l

    A range is an array like items so there are no ranges in here. If you mean “zip >= '99501' && zip <= '99950'”, than that is not a range, it is two conditions linked with an “and” conditional. >= means greater than or equal to and <= means less than or equal to. Normally in most languages the zip variable would have to have a value of an integer, but javascript's variables are dynamic.

  • charles

    “IMHO” maybe but brilliant nevertheless

  • charles

    “IMHO” maybe but brilliant nevertheless

  • http://www.northdrugstore.co canada pharmacy

    Conversational hypnosis is a technique that is not easily mastered. If you don't know the secrets behind it, you will most likely fail when you try it. The reason it is so difficult is because when you are trying to induce your subject into the trance state, they will often resist you if you make your motives too clear.

  • Al

    That’s accurate in some contexts (like spreadsheets). It’s perfectly acceptable to refer to this test as a range.

Back to top