/    Sign up×
Community /Pin to ProfileBookmark

HELP!! Newbie to Javascript

I’m trying to create a simple validation script using JavaScript. I want it for use with zipcodes entered into a form.

I have a business that will only deliver to certain zipcodes in the area. If the user enters a zipcode in the form that’s not one of these, I’m giving them an alert that says “Sorry, we do not deliver to your area at this time.”

I know the solution must be simple, but its still eluding me! Here’s my code:

NOTE: My form’s name is “form1”, the text field for zipcode entry is named “zip”…

<script language=”javascript”>
function verify_zip(){
var zipcode = form1.zip.value;

if ((zipcode != “81601”)||(zipcode != “81602”)||(zipcode != “81623”)||(zipcode != “81621”)||(zipcode != “81654”)||(zipcode != “81615”)||(zipcode != “81611”)||(zipcode != “81612”)||(zipcode != “81647”))
{
alert(“Sorry, we do not deliver to your area at this time.”);
}
else if ((zipcode == null)||(zipcode == “”))
{
alert(“You must enter a zipcode.”);
}
else
{
alert(“good zip”);
}
}
</script>

The last “else” is just temporary. I put it in to aid me in testing. I call this function using the “onBlur” event in the text field. When I test it, no matter what I enter in the zipcode field I get the alert “Sorry, we do not deliver to your area at this time.”

Please, if you could tell me what I’m doing wrong I would GREATLY appreciate it!!! ? ? ? ?

igasper10

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinJan 06.2006 — change all your OR's ( || ) for AND's ( && )
Copy linkTweet thisAlerts:
@MigJan 06.2006 — You only want to change the ORs in the first bit before the alert, however. The bit about the zip code being null or blank is correct.
Copy linkTweet thisAlerts:
@Tweak4Jan 06.2006 — This is one of those fun times where the English language and logic don't quite line up. We tend to think "Not A, or B, or C, or D", which usually results in the "NOT OR" logic in the OP. However in programming, you must think "Not A, and not B, and not C...", which will yield the results you are looking for.

For example, if you're looking for a value that is something other than 1 or 2, consider the following:
<i>
</i>if ((x != 1)||(x != 2) return true;

This will always return true, since no number can satisfy both parts of the statement (if x is 1, it can't equal 2 and vice versa. Either way, at least one part of the criteria is true, so the final result is true. If x is neither 1 or 2, both parts are true, and the total result is still true)

It takes a little getting used to, but you'll get the hang of it.
×

Success!

Help @igasper10 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...