Skip to main content
Participant
January 10, 2021
Question

Zip Code defining City & State fields

  • January 10, 2021
  • 2 replies
  • 500 views

I am using the following javascript to prepopulate "City" and "State" fields.

 

var ZipData = {
02860:{state: "RI", city:"PROVIDENCE"},
02901:{state: "RI", city:"PROVIDENCE"},
02940:{state: "RI", city:"PROVIDENCE"},
03101:{state: "NH", city:"MANCHESTER"},
03106:{state: "NH", city:"HOOKSET"},
03253:{state: "NH", city:"MEREDITH"},
04240:{state: "ME", city:"LEWISTON"},
18002:{state: "PA", city:"LEHIGH VALLEY"},
21152:{state: "MD", city:"SPARKS"},
21203:{state: "MD", city:"BALTIMORE"},
21921:{state: "MD", city:"ELKTON"},
22901:{state: "VA", city:"CHARLOTTESVILLE"},
23173:{state: "VA", city:"RICHMOND"},
23452:{state: "VA", city:"VIRGINIA BEACH"},
27253:{state: "NC", city:"GRAHAM"},
27889:{state: "NC", city:"WASHINGTON"},
28269:{state: "NC", city:"CHARLOTTE"},
29202:{state: "SC", city:"COLUMBIA"},
29606:{state: "SC", city:"GREENVILLE"},
30096:{state: "GA", city:"DULUTH"},
30317:{state: "GA", city:"ATLANTA"},
30319:{state: "GA", city:"BROOKHAVEN"},
30348:{state: "GA", city:"ATLANTA"},
31004:{state: "GA", city:"BOLINGBROKE"},
32901:{state: "FL", city:"MELBOURNE"},
33483:{state: "FL", city:"DELRAY BEACH"},
33631:{state: "FL", city:"TAMPA"},
33785:{state: "FL", city:"INDIAN ROCKS BEACH"},
33810:{state: "FL", city:"LAKELAND"},
34113:{state: "FL", city:"NAPLES"},
34429:{state: "FL", city:"CRYSTAL RIVER"},
34698:{state: "FL", city:"DUNEDIN"},
35022:{state: "AL", city:"BESSEMER"},
43065:{state: "OH", city:"POWELL"},
43123:{state: "OH", city:"GROVE CITY"},
43420:{state: "OH", city:"FREMONT"},
44012:{state: "OH", city:"AVON LAKE"},
44306:{state: "OH", city:"AKRON"},
44839:{state: "OH", city:"HURON"},
45177:{state: "OH", city:"WILMINGTON"},
46361:{state: "IN", city:"MICHIGAN CITY"},
46504:{state: "IN", city:"BOURBON"},
46537:{state: "IN", city:"LAPAZ"},
46565:{state: "IN", city:"MISHAWAKA"},
46619:{state: "IN", city:"SOUTH BEND"},
46923:{state: "IN", city:"DELPHI"},
47918:{state: "IN", city:"ATTICA"},
52001:{state: "IA", city:"DUBUQUE"},
53151:{state: "WI", city:"NEW BERLIN"},
53214:{state: "WI", city:"WEST ALLIS"},
54751:{state: "WI", city:"MENOMONIE"},
55439:{state: "MN", city:"MINNEAPOLIS"},
55440:{state: "MN", city:"MINNEAPOLIS"},
55987:{state: "MN", city:"WINONA"},
57718:{state: "SD", city:"BLACK HAWK "},
57719:{state: "SD", city:"BOX ELDER"},
60002:{state: "IL", city:"ANTIOCH"},
60010:{state: "IL", city:"BARRINGTON"},
60025:{state: "IL", city:"GLENVIEW"},
60107:{state: "IL", city:"STREAMWOOD"},
60302:{state: "IL", city:"OAK PARK"},
60402:{state: "IL", city:"BERWYN"},
60430:{state: "IL", city:"HOMEWOOD"},
60438:{state: "IL", city:"LANSING"},
60546:{state: "IL", city:"RIVERSIDE"},
60559:{state: "IL", city:"WESTMONT"},
60614:{state: "IL", city:"CHICAGO"},
60616:{state: "IL", city:"CHICAGO"},
60618:{state: "IL", city:"CHICAGO"},
61709:{state: "IL", city:"BLOOMINGTON"},
62525:{state: "IL", city:"DECATUR"},
63101:{state: "MO", city:"ST LOUIS"},
63132:{state: "MO", city:"ST LOUIS"},
74134:{state: "OK", city:"TULSA"},
78288:{state: "TX", city:"SAN ANTONIO"},
80002:{state: "CO", city:"ARVADA"},
90212:{state: "CA", city:"BEVERLY HILLS"},
92660:{state: "CA", city:"NEWPORT BEACH"},
93063:{state: "CA", city:"SIMI VALLEY"},
95899:{state: "CA", city:"SACRAMENTO"},
97219:{state: "OR", city:"PORTLAND"}};
function SetCityValue(cZip)
{
this.getField("City").value = ZipData[cZip].city;

this.getField("State").value = ZipData[cZip].state;;
}

 

 

The script works fine for only those zip codes beginning with 1-9.  Zip codes beginning with a 0 fail to prepopulate.

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
January 10, 2021

Make sure to use the valueAsString property when accessing the value of the field that you use for cZip, not value.

Bernd Alheit
Community Expert
Community Expert
January 10, 2021

How does you use/call the function SetCityValue?

Use " at begin and end of the zip codes.

rauensenAuthor
Participant
January 19, 2021

This is the Custom Keystroke Script for my zip code field:

 


if( event.willCommit )
{
if(event.value == " ")
this.resetForm(["City"]);
else
SetCityValue(event.value);
}
AutoTab(this, event, "City");