JavaScript zip code to state conversion
zacharyburt.com
zacharyburt.com
It's also going to be wrong for military addresses, as I understand it. [2]
[1]: http://en.wikipedia.org/wiki/ZIP_code#By_geography
[2]: http://www.usps.com/supportingourtroops/addressingtips.htm
Unless you're going to keep your list up to date, this code will break from time to time.
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
endIt just converts first digit of zip code to list of states to which it could apply.
[#][##][##]
[State][County/Parish][Area]
Instead you should be giving your users a choice made on exact facts instead of probabilities.Our data could have been inaccurate in some cases (but I seem to remember testing a few hundred addresses vs google's geocoder and getting correct results).
The demographic wasn't very tech focused, so it could be that they were either weirded out, or (in surprisingly many cases) just didn't know their zip code.
Anyway, just make sure to test this before blindly assuming that it increases usability in your case.
A good way for the server to detect user error would be to use IP2location. Unless they're using a proxy or VPN, you could add something that would ask the user to double check if their IP location was more than 100 miles from their zip code. But the user could always override that warning.
[City], [State] [Zip]
The state (and often even the city; there are free datasets for this though they're not perfectly reliable and more expensive) can be inferred from the zipcode, so it seems logical that we ought to prompt first for the zip and then help users along by filling the state on their behalf. I suspect his is not done because people are so used to the City -> State -> Zip pattern, asking them to fill the zip first just breaks their expected flow; a "surprise" that makes them think.
I used to work on a case management system for legal aid attorneys that did these lookups and we had to explicitly train them to enter the zip code first (changing field order or tab order didn't help) -- otherwise, they'd never have used it; and some of them still refused to use it, but those who did appreciated the time savings.
It's also interesting to think about text input versus dropdown select if you decide to go for manual state entry. I prefer just typing in the abbreviation, but I'm not sure that's suitable for a wide audience.
I'm not sure many people actually know their full 9 digit zip. Certainly not many of my customers have bothered entering it.
http://en.wikipedia.org/wiki/Postal_codes_in_Canada#Componen...
So here's my solution:
def state?(zip)
zip_code_list = File.open('zips.txt')
suggested_list = zip_code_list.grep(/(.*"#{zip}\d+".*)/)
endIt seemed like a disruptive flow to me too, until I asked for zip without showing any other address fields. On change, the address and prepopulated city and state fields slid down above it and address got focus.
Objective: Deliver complete address with minimal keystrokes.
Constraint: User can type whatever they want.
Approach: One text box for address. Reevaluate with each new key entered. Use lots of indexed tables (zip codes, cities, states) for verification. Reformat when they leave the box. Provide help, autofill, undo, dropdowns, and error message feedback.
Some of the logic:
- 5 consecutive integers: verify zip code. Autofill city and state. If multiple cities, dropdown for choice.
- 2 alphas and a space: verify state.
- 3+ alphas: verify city. Dropdown for State.
You kinda get the idea. There's probably a lot more optimization that could be done. It's worth it because it's used in so many places.