我正在尝试创建一个在人们插入特定邮政编码或邮政编码范围时弹出错误的表单。
例如:
如果潜在的潜在客户填写表格并输入华盛顿州邮政编码,那么我想要弹出一个错误,说我们不提供该州的产品。
我搜索了Google和Stackoverflow,但仍然没有找到代码的实际示例...我已经看到了类似的但不完全是我的示例所说的。
我正在寻找的例子可以是javascript或jquery
答案 0 :(得分:4)
您可能最好测试一下您支持的邮政编码......
function zipToState(zip){
var state, i= 0, next, statezips= [
[1000, 2792, 'MA'], [2800, 2941, 'RI'], [3030, 3898, 'NH'], [3900, 4993, 'ME'],
[5000, 5496, 'VT'],[5500, 5545, 'MA'], [5600, 5908, 'VT'], [6000, 6390, 'CT'],
[6389, 6391, 'NY'], [6400, 6929, 'CT'],[7000, 8990, 'NJ'],
[10000, 14976, 'NY'], [15000, 19641, 'PA'], [19700, 19981, 'DE'],
[20000, 20040, 'DC'], [20039, 20042, 'VA'], [20039, 20168, 'VA'], [20041, 20600, 'DC'],
[20041, 20043, 'VA'], [20330, 20332, 'MD'], [20334, 20798, 'MD'], [20798, 20800, 'DC'],
[20811, 21931, 'MD'], [22000, 24659, 'VA'], [24700, 26887, 'WV'], [27005, 28910, 'NC'],
[29000, 29949, 'SC'], [30000, 32000, 'GA'], [32003, 34998, 'FL'], [35003, 36926, 'AL'],
[37009, 38590, 'TN'], [38600, 39777, 'MS'], [39900, 39902, 'GA'], [40002, 42789, 'KY'],
[43000, 46000, 'OH'], [46000, 47998, 'IN'], [48000, 49972, 'MI'], [50000, 52810, 'IA'],
[53000, 54991, 'WI'], [55000, 56764, 'MN'], [57000, 57800, 'SD'], [58000, 58857, 'ND'],
[59000, 59938, 'MT'], [60000, 63000, 'IL'], [63000, 65900, 'MO'], [66001, 67955, 'KS'],
[68000, 68119, 'NE'], [68118, 68121, 'IA'], [68121, 69368, 'NE'], [70000, 71233, 'LA'],
[71232, 71234, 'MS'], [71233, 71498, 'LA'], [71600, 72960, 'AR'], [73000, 73200, 'OK'],
[73300, 73302, 'TX'], [73400, 74967, 'OK'], [75000, 75502, 'TX'], [75501, 75503, 'AR'],
[75502, 80000, 'TX'], [80000, 81659, 'CO'], [82000, 83129, 'WY'], [83200, 83877, 'ID'],
[84000, 84785, 'UT'], [85000, 86557, 'AZ'], [87000, 88442, 'NM'], [88509, 88590, 'TX'],
[88900, 89884, 'NV'], [90000, 96163, 'CA'], [96700, 96899, 'HI'], [97000, 97921, 'OR'],
[98000, 99404, 'WA'], [99500, 99951, 'AK']
];
if(/^\d{5}$/.test(zip)){
zip= Number(zip.substring(0,5));
while(i<72){
next= statezips[i++];
if(zip> next[0] && zip< next[1]) return next[2];
}
}
return '';
}
zipToState('04355')
/* returned value: (String)
ME
*/
答案 1 :(得分:2)
考虑ID为zipCode的输入元素
<input id="zipCode" name="zipCode" />
我们可以通过执行类似
的操作来捕获它的价值var zipCode = $("#zipCode").val();
考虑建立一个类似......
的阵列var acceptableZipCodes = ["53782", "11709", "97035", "86531"];
我们可以测试一下zipCode的值是否在数组中,如果不是,我们将返回一个警告。
if($.inArray(zipCode, acceptableZipCodes)){
//do nothing, or do whatever you want
//we have a return true here.
}else{
alert("Sorry. we don't offer products in that state.");
}
现在,我们应该把我们的代码放在一个事件中。让我们使用keyup函数,但因为我们不想检查每个值,所以我们只检查第5个keyup。
var acceptableZipCodes = ["53782", "11709", "97035", "86531"];
$("#zipCode").live('keyup', function(){
var zipCode = $(this).val();
if(zipCode.length >4){
if($.inArray(zipCode, acceptableZipCodes)){
//do nothing, or do whatever you want
//we have a return true here.
}else{
alert("Sorry. we don't offer products in that state.");
}
}
});
答案 2 :(得分:1)
加载您在数组中提供的一组zipcodes。如果您使用的是jQuery:
var zip= $('#zipcode').val();
if( $.inArray( zip, zipCadeArray)===-1){
alert("We don't service that zip")
}
将警报修改为您想要的行为