我对 Javascript 做的不多。
我想做的是有一个表格,用户可以在其中输入他们的邮政编码,以查看他们是否在选定数量的邮政编码(客户的服务区域)内。
如果他们的邮政编码在列表中,我想将他们发送到一个 URL 以安排约会。如果它不在列表中,我想要某种警报,上面写着“抱歉,您的邮政编码不在我们的服务区域”。
我有一些适应这种工作的东西,但无论他们输入什么,都会将用户发送到页面。
任何帮助是极大的赞赏!
<form name = "myform" action="http://www.google.com/">
Enter Your Zip Code <input type = "text" name = "zip" size = "5" maxlength = "5" onchange = "checkZip()">
<button id="submit">Submit</button>
</form>
<script type = "text/javascript">
function checkZip() {
var z = document.myform.zip;
var zv = z.value;
if (!/^\d{5}$/.test(zv)) {
alert("Please enter a valid Zip Code");
document.myform.zip.value = "";
myfield = z; // note myfield must be a global variable
setTimeout('myfield.focus(); myfield.select();', 10); // to fix bug in
// Firefox
return false;
}
var codes = [ 12345, 12346, 12347, 12348, 12349, 12350 ]; // add as many
// zip codes as
// you like,
// separated by
// commas (no
// comma at the
// end)
var found = false;
for ( var i = 0; i < codes.length; i++) {
if (zv == codes[i]) {
found = true;
break;
}
}
if (!found) {
alert("Sorry, the Zip Code " + zv + " is not covered by our business");
document.myform.zip.value = "";
return false;
} else {
alert("Press okay to go forward to schedule an appointment");
}
}
</script>