Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is the Question in two parts:

I am having problems creating a zip-code look-up in javascript. The first part is

1) having javascript recongnize that a zip-code is linked to a state, and possibly maintain that data. The second part is to

2) Have the javascript output the state in the second field text "State Result" which is displayed to the user.

The script and site can be found here:

Edit fiddle - JSFiddle[^](This contains the entire code, and is not a download. Pasted on JSFiddle as the full code is too long for CodeProject. Similar to pastebin).

References to Nodes, External Websites (posting an external site for examples is okay, linking in code is not), PhP, SQL Databases, external Program downloads,extensions, are unfortunately not allowed. This script consist of using the HTML/Javascript languages only.

-----Here is part of the javascript----:
JavaScript
function validateZIP(zipfield, statefield) {
var valid = "0123456789-";
var hyphencount = 0;


What I have tried:

JavaScript
function GetZip(90001, 90002){
  return California, California
}

document.getElementById("")=GetZip (9001, 9002)

As seen above, I am attempting to use an object to call California by 9001 with the value return being California to the user on a single page.


I have tried the concept of functions, however dictionaries is a new concept.

There are many zipcodes I need a shorter and cleaner/easier way of calling zip-codes to be searched on the site with the output as corresponding states.
Posted
Updated 30-Oct-17 6:24am
v8
Comments
Richard MacCutchan 29-Oct-17 4:41am    
Your question was most likely closed because you had a link to an external site in it. And you still have that, so this question is also at risk. Please edit the question, remove the link, and provide the actual text of the code that does not work. Do not expect people to download your code from external sites and try to figure out what is wrong with it.
Richard MacCutchan 29-Oct-17 13:13pm    
The first thing you need is a dictionary that contains all the states and the associated zip codes. I assume that US zip codes for each state are in some sort of sequence, so you just need the lowest and highest values. Then all you need to do is search the dictionary for a range that the code lies within, and that will identify your state. But I am sure it is much easier to use some ready made API service to do it.
Member 13491039 29-Oct-17 13:19pm    
Yes, I do have the zipcodes and the corresponding states converted into dictionary. I'm not exactly sure how dictionaries work, but I have used this open-source dictionary convertor website that converts should anyone else wish to use it.

https://shancarter.github.io/mr-data-converter/
Richard MacCutchan 29-Oct-17 13:20pm    
What does that have to do with your question?
Member 13491039 29-Oct-17 17:54pm    
The convertor changes regular text into JS Dictionary form, including ,'s.

 
Share this answer
 
Comments
Member 13491039 29-Oct-17 13:29pm    
Thanks for your response! But link to the API is currently offline.
Richard MacCutchan 29-Oct-17 13:35pm    
Not from here.
If you follow this link Free ZIP code map, zip code lookup, and zip code list[^], you will note, about mid-page, the topic "Matching ZIP Codes with States, Counties, and Cities". Within this text it give some information that should let you realize that, in principal, your task is impossible!

Here's the quote:
Quote:
the ZIP code "boundary" will cross the state lines. ZIP codes don't usually cross state lines, but some do (65733, 71749, and 73949 are good examples).

There's info there that can make a pretty efficient engine for you - but since it won't always be true you can never fix the problem (unless you go to zip+4)
 
Share this answer
 
Comments
Member 13491039 31-Oct-17 21:08pm    
Thank you! I have seen that problem especially when the zip codes jump around so they aren't always in a pattern. I have found a workable solutions using if else's in a range. So now the code actually works within HTML and JavaScript without external networks or sites involved
else if (Statenames>=70001 && Statenames<=71497){
           return "LA"
               }


By adding a range using else if statements and using the method before this line by declaring a function, I was able to successfully create a zipcode lookup. Though not 100% accurate as zip-codes can jump from one range to another, it got 95% of it and including the smaller, lost zips within the range of the bigger ones. This was also done without an API/External code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900