1

我正在尝试创建一个表单,当人们插入特定的邮政编码或邮政编码范围时会弹出错误。

例如:

如果潜在的潜在客户填写表格并输入华盛顿州的邮政编码,那么我希望弹出一个错误,说明我们不提供该州的产品。

我已经搜索了 Google 和 Stackoverflow,但仍然没有找到一个有效的代码示例......我看到了一些类似但与我的示例所说的不完全一样的代码。

我正在寻找的示例可能是 javascript 或 jquery

4

3 回答 3

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
*/
于 2012-02-28T21:56:27.240 回答
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.");
     } 
   }
});    
于 2012-02-28T21:52:44.850 回答
1

加载您在数组中提供的邮政编码数组。如果您使用的是 jQuery:

var zip= $('#zipcode').val();

if( $.inArray( zip, zipCadeArray)===-1){
  alert("We don't service that zip")
}

将警报修改为您想要的行为

于 2012-02-28T21:52:01.717 回答