PC-Dad said:
I'd like to make a form in which the user would input her zip code.
When they submit then the script would compare to the list of zips that
I service. If their zip is included re-direct to page A or if not
included re-direct to page B.
Would anyone be able to assist me here?
It would probably be better to do that on the server side, if possible,
because you wouldn't have any practical limit to the number of zip codes
or other factors that you use to decide which page to show.
Here's one example that allows you to use an "x" as a wildcard character
and number options in square brackets. The entry "8080[79]" matches
either a 7 or a 9 in the last position. "876x[123]" would match any digit
in the 4th position, and any of 1, 2, or 3 in the 5th, etc.
Note that any invalid entry is treated as a zip code that isn't in the
area.
<html>
<head>
<script type="text/javascript">
zipList= [ "12345", "23456", "345xx", "4567x",
"56789", "56780", "8080[79]", "7xx01"
];
function inArea(zip) {
for(var i=0;i<zipList.length;i++) {
if ((new RegExp("^\\s*"
+zipList[i].replace(/x/g,"\\d")
+"\\s*$").test(zip))) {
return true;
}
}
return false;
}
function redirect(zip) {
if(inArea(zip)) {
location="http://www.google.com";
} else {
location="http://maps.google.com/maps?q="+zip+"&spn=0.1,0.1";
}
}
</script>
</head>
<body>
<form onsubmit="return false">
Enter 5-digit U.S. zip code: <input name="zip"><br>
<input type="button" value="test" onclick="redirect(this.form.zip.value)">
</form>
</html>