/    Sign up×
Community /Pin to ProfileBookmark

ZipCode Object

I have gone at this several different ways and can’t get it to work.

I have a script which loads the zipcodes for Ohio into a javascript object, then when someone enters a zipcode on the page onBlur triggers a function which searches the Object to find the city and state based on the zipcode and then prefills the appropriate text boxes on the form.
THIS WORKS GREAT as long as you live in Ohio.

Problem: If you enter a zipcode for a different state I can’t seem to evaluate to trigger an alert.

Obviously it doesn’t find an instance in the object. So I have tried to evaluate. For example–

function zipLookup(){
var zipc = window.document.submitq.txtZipCode.value;
var city = zipArray[zipc].city;

if(city == null || city ==”undefined”){
alert(“Not an Ohio ZipCode”);
return false;
}
}

I can never get this thing to alert! Thanks for any help!!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Mar 26.2004 — why not try:[code=php]function zipLookup(){
var zipc = window.document.submitq.txtZipCode.value;
var city = ""
var zipArray = new Array
zipArray[0] = {city:"Cleveland",zip:"12345"}
// same pattern

for(var i=0;i<zipArray.length;i++) {
if(zipArray[i].zip = zipc
city = zipArray[zipc].city;
}
}

if(city.length == 0) {
alert("Not an Ohio ZipCode");
return false;
}
}[/code]
Copy linkTweet thisAlerts:
@ianjkingauthorMar 26.2004 — Ok, I gave it a whirl, here is all the code, part one creates the JS object and fills it from the database using Java in a JSP page. I then run it through a process to simulate a Hash Table for speed in look up.

Part two is my zipcode lookup text where I am having the problem.

<%

//////LOAD JAVASCRIPT VARIABLES AND FUNCTIONS///////////////////

selectStr = "SELECT zip, city, state FROM zipcity ORDER BY zip";

rs = Stmt.executeQuery(selectStr);

%>

<script language="JavaScript" type="text/JavaScript">

<!--

var zipArray = [<%

while(rs.next()){

tmpStr = rs.getString("zip");

tmpStr1 = rs.getString("city");

tmpStr2 = rs.getString("state"); %>

{zipcode:"<%=tmpStr %>", city:"<%=tmpStr1 %>", state:"<%=tmpStr2 %>"}, <%

} %>

{zipcode:"none", city:"none", state:"none"}];

for (var t=0; t < zipArray.length; t++){

zipArray[zipArray[t].zipcode] = zipArray[t];

}


<!--This looks up the zipcode and returns city and state -->

function zipLookup(form, zipc, city2, state2){

var zip = form.elements[zipc].value;

if(!/^[0-9]{5}$/.test(zip)) {

alert("Invalid US Zip Code (Format: #####)")

form.elements[zipc].focus()

form.elements[zipc].select()

return false

}

var city1 = zipArray[zip].city;

var state1 = zipArray[zip].state;

form.elements[city2].value = city1;

form.elements[state2].value = state1;

}


I think the problem with the length issue is the Hash Table approach. Any help is greatly appreciated!!
×

Success!

Help @ianjking spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...