<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Validate Zip Code</title>
<script type="text/javascript">
function IsValidZipCode(zip) {
var isValid = /20132/.test(zip);
if (isValid)
alert('Valid ZipCode');
else {
alert('yep')
}
}
</script>
</head>
<body>
<form>
<input id="txtZip" name="zip" type="text" /><br />
<input id="Button1" type="submit" value="Check My Zipcode"
onclick="IsValidZipCode(this.form.zip.value)" />
</form>
</body>
</html>
我需要使用它来允许用户转到一个页面,该页面显示“抱歉,我们无法为您所在的区域提供服务”,或者转到另一个页面,该页面显示“是的,我们可以根据是否列出了他们的邮政编码为您的区域提供服务”。
另外,我如何在 isValid = 行中添加多个邮政编码?
最佳答案
设置window.location.href =“your url here”;
回答了您问题的第一部分。
"also how can i add more than one zip code in the isValid = line?"
如果您想坚持使用正则表达式测试,您可以使用正则表达式或|
:
var isValid = /^(20132|20133|20200|90210|etc)$/.test(zip);
请注意,我还添加了 ^
和 $
来匹配输入字符串的开头和结尾 - 按照您的方式,如果用户输入了包含该代码的较长字符串,例如,"abc20132xyz"
将匹配。
不过,我更倾向于在服务器端进行此验证。
关于php - 如何更改 JavaScript 的输出以直接转到另一个页面而不是警报窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13189739/