编写 JavaScript 邮政编码验证函数

发布于 2024-07-14 11:35:41 字数 898 浏览 8 评论 0原文

我想编写一个 JavaScript 函数来验证邮政编码,通过检查邮政编码是否确实存在。 以下是所有邮政编码的列表:

http://www.census.gov /tiger/tms/gazetteer/zips.txt (我只关心第二列)


这确实是一个压缩问题。 我想做这个是为了好玩。 好的,现在这已经不成问题了,这里是我能想到的对直接哈希表的优化列表,请随意添加我没有想到的任何内容:

  • 将邮政编码分成两部分,前 2 位数字和最后 3 位数字。
  • 制作一个巨大的 if-else 语句,首先检查前 2 位数字,然后检查最后 3 位数字内的范围。
  • 或者,将拉链隐藏成十六进制,看看我是否可以使用较小的组做同样的事情。
  • 查明在所有有效邮政编码范围内是否存在更多有效邮政编码与无效邮政编码。 针对较小的群体编写上述代码。
  • 将哈希值分解为单独的文件,并在用户输入邮政编码时通过 Ajax 加载它们。 因此,也许分为两部分,第一部分用于前 2 位数字,第二部分用于最后 3 位数字。

最后,我计划使用另一个程序而不是手动生成 JavaScript 文件。

编辑:性能在这里很重要。 我确实想用这个,如果它不烂的话。 JavaScript 代码执行+下载时间的性能。

编辑 2:请仅使用 JavaScript 解决方案。 我无法访问应用程序服务器,另外,这会使这成为一个完整的其他问题=)

I would like to write a JavaScript function that validates a zip code, by checking if the zip code actually exists. Here is a list of all zip codes:

http://www.census.gov/tiger/tms/gazetteer/zips.txt (I only care about the 2nd column)


This is really a compression problem. I would like to do this for fun. OK, now that's out of the way, here is a list of optimizations over a straight hashtable that I can think of, feel free to add anything I have not thought of:

  • Break zipcode into 2 parts, first 2 digits and last 3 digits.
  • Make a giant if-else statement first checking the first 2 digits, then checking ranges within the last 3 digits.
  • Or, covert the zips into hex, and see if I can do the same thing using smaller groups.
  • Find out if within the range of all valid zip codes there are more valid zip codes vs invalid zip codes. Write the above code targeting the smaller group.
  • Break up the hash into separate files, and load them via Ajax as user types in the zipcode. So perhaps break into 2 parts, first for first 2 digits, second for last 3.

Lastly, I plan to generate the JavaScript files using another program, not by hand.

Edit: performance matters here. I do want to use this, if it doesn't suck. Performance of the JavaScript code execution + download time.

Edit 2: JavaScript only solutions please. I don't have access to the application server, plus, that would make this into a whole other problem =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

赏烟花じ飞满天 2024-07-21 11:35:41

您可以做一些不可思议的事情,并将代码视为数字(请记住,它实际上不是数字)。 将列表转换为一系列范围,例如:

zips = [10000, 10001, 10002, 10003, 23001, 23002, 23003, 36001]
// becomes
zips = [[10000,10003], [23001,23003], [36001,36001]]
// make sure to keep this sorted

然后进行测试:

myzip = 23002;
for (i = 0, l = zips.length; i < l; ++i) {
    if (myzip >= zips[i][0] && myzip <= zips[i][1]) {
        return true;
    }
}
return false;

这只是使用非常简单的线性搜索 (O(n))。 如果对列表进行排序并使用二分查找,则可以实现 O(log n)。

You could do the unthinkable and treat the code as a number (remember that it's not actually a number). Convert your list into a series of ranges, for example:

zips = [10000, 10001, 10002, 10003, 23001, 23002, 23003, 36001]
// becomes
zips = [[10000,10003], [23001,23003], [36001,36001]]
// make sure to keep this sorted

then to test:

myzip = 23002;
for (i = 0, l = zips.length; i < l; ++i) {
    if (myzip >= zips[i][0] && myzip <= zips[i][1]) {
        return true;
    }
}
return false;

this is just using a very naive linear search (O(n)). If you kept the list sorted and used binary searching, you could achieve O(log n).

人生百味 2024-07-21 11:35:41

我想编写一个验证邮政编码的 JavaScript 函数

保持更新以便在任何时候都不会拒绝某人真正有效的邮政编码,这可能比其价值更多的努力。 您还可以尝试外部服务,或者像其他人一样,只接受任何 5 位数字!

这是我能想到的针对直接哈希表的优化列表

很抱歉破坏了潜在的乐趣,但是您可能无法获得比 JavaScript 的对象用作哈希表时提供的更好的实际性能。 对象成员访问是JS中最常见的操作之一,将会被超级优化; 构建自己的数据结构不太可能打败它,即使从计算机科学的角度来看它们可能是更好的结构。 特别是,使用“数组”的任何内容都不会像您想象的那样执行,因为数组实际上是作为对象(哈希表)本身实现的。

话虽如此,如果您只需要知道“有效与否”,一个可能的空间压缩工具是使用 100000 位的位字段,并将其打包到字符串中。 例如,对于只有 100 个邮政编码的空间,其中代码 032-043 是“有效”:

var zipfield= '\x00\x00\x00\x00\xFF\x0F\x00\x00\x00\x00\x00\x00\x00';
function isvalid(zip) {
    if (!zip.match('[0-9]{3}'))
        return false;
    var z= parseInt(zip, 10);
    return !!( zipfield.charCodeAt(Math.floor(z/8)) & (1<<(z%8)) );
}

现在我们只需找出将位字段获取到脚本的最有效方法。 上面的天真“\x00”填充版本效率相当低。 减少这种情况的传统方法是例如。 对它进行 base64 编码:

var zipfield= atob('AAAAAP8PAAAAAAAAAA==');

这将使 100000 个标志减少到 16.6kB。 不幸的是 atob 仅适用于 Mozilla,因此其他浏览器需要额外的 base64 解码器。 (这并不太难,但是解码的启动时间要长一些。)也可以使用 AJAX 请求来传输直接二进制字符串(以 ISO-8859-1 文本编码到responseText)。 这将使其减少到 12.5kB。

但实际上,只要您使用 mod_deflate 提供脚本,任何东西(即使是幼稚的版本)都可以,这会压缩大量冗余,并且对于所有长范围的“无效”重复“\x00” ' 代码。

I would like to write a JavaScript function that validates a zip code

Might be more effort than it's worth, keeping it updated so that at no point someone's real valid ZIP code is rejected. You could also try an external service, or do what everyone else does and just accept any 5-digit number!

here is a list of optimizations over a straight hashtable that I can think of

Sorry to spoil the potential Fun, but you're probably not going to manage much better actual performance than JavaScript's Object gives you when used as a hashtable. Object member access is one of the most common operations in JS and will be super-optimised; building your own data structures is unlikely to beat it even if they are potentially better structures from a computer science point of view. In particular, anything using ‘Array’ is not going to perform as well as you think because Array is actually implemented as an Object (hashtable) itself.

Having said that, a possible space compression tool if you only need to know 'valid or not' would be to use a 100000-bit bitfield, packed into a string. For example for a space of only 100 ZIP codes, where codes 032-043 are ‘valid’:

var zipfield= '\x00\x00\x00\x00\xFF\x0F\x00\x00\x00\x00\x00\x00\x00';
function isvalid(zip) {
    if (!zip.match('[0-9]{3}'))
        return false;
    var z= parseInt(zip, 10);
    return !!( zipfield.charCodeAt(Math.floor(z/8)) & (1<<(z%8)) );
}

Now we just have to work out the most efficient way to get the bitfield to the script. The naive '\x00'-filled version above is pretty inefficient. Conventional approaches to reducing that would be eg. to base64-encode it:

var zipfield= atob('AAAAAP8PAAAAAAAAAA==');

That would get the 100000 flags down to 16.6kB. Unfortunately atob is Mozilla-only, so an additional base64 decoder would be needed for other browsers. (It's not too hard, but it's a bit more startup time to decode.) It might also be possible to use an AJAX request to transfer a direct binary string (encoded in ISO-8859-1 text to responseText). That would get it down to 12.5kB.

But in reality probably anything, even the naive version, would do as long as you served the script using mod_deflate, which would compress away a lot of that redundancy, and also the repetition of '\x00' for all the long ranges of ‘invalid’ codes.

云巢 2024-07-21 11:35:41

我使用 Google Maps API 来检查邮政编码是否存在。

更准确。

I use Google Maps API to check whether a zipcode exists.

It's more accurate.

她说她爱他 2024-07-21 11:35:41

假设您已经在排序数组中获得了 zip(如果您控制数据结构的生成,这似乎是公平的),请查看简单的二分搜索是否足够快。

Assuming you've got the zips in a sorted array (seems fair if you're controlling the generation of the datastructure), see if a simple binary search is fast enough.

最美的太阳 2024-07-21 11:35:41

那么...您正在进行客户端验证并想要优化文件大小? 您可能无法击败一般压缩。 幸运的是,大多数浏览器都支持 gzip,因此您可以免费使用它。

一个简单的 json 编码字典或列表(按排序顺序包含邮政编码)并在字典上查找怎么样? 它会压缩得很好,因为它是一个可预测的序列,因为它是 json,所以可以使用浏览器内置的解析器轻松导入,而且查找也可能非常快,因为这是一个 JavaScript 原语。

So... You're doing client side validation and want to optimize for file size? you probably cannot beat general compression. Fortunately, most browsers support gzip for you, so you can use that much for free.

How about a simple json coded dict or list with the zip codes in sorted order and do a look up on the dict. it'll compress well, since its a predictable sequence, import easily since it's json, using the browsers in-built parser, and lookup will probably be very fast also, since that's a javascript primitive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文