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