【问题标题】:How to validate that zip code enter by user is correct US zipcode如何验证用户输入的邮政编码是正确的美国邮政编码
【发布时间】:2010-07-13 11:24:10
【问题描述】:

我想验证用户输入的邮政编码是否有效。

for example 用户输入了009876654,它是无效的,那么应该给出一个错误信息。

我知道我可以使用 javascript regulr expression 或使用 ajax-zip-code-database 来做到这一点

但我不想要上述任何一种。我需要一些插件之类的东西,它可以向某些在线应用程序发送请求以检查它是否有效。我想要这个,因为我不想在未来有邮政编码或新邮政编码的变化 -添加代码。

附: :- 我不想使用javascriptajax-zip-code-database

【问题讨论】:

  • 您的意思是您根本不想使用 javascript(调用 Web 服务)还是只想使用它的正则表达式部分?
  • @Tommy:- 只是它的正则表达式部分

标签: ruby-on-rails ruby validation ruby-on-rails-plugins zipcode


【解决方案1】:

webservicex 有一个 Web 服务,它可以为您提供来自 GET 甚至 POST 调用的 XML 结果。我从未使用过它,但它似乎正是您想要的。

不存在的邮政编码返回一个空数据集

wget http://www.webservicex.net/uszip.asm /GetInfoByZIP?USZip=60001

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
  <Table>
  <CITY>Alden</CITY>
  <STATE>IL</STATE>
  <ZIP>60001</ZIP>
  <AREA_CODE>815</AREA_CODE>
  <TIME_ZONE>C</TIME_ZONE>
</Table>
</NewDataSet>

【讨论】:

    【解决方案2】:

    假设您的应用程序在商业上与其使用条款兼容,我想知道您是否可以使用 Google 的 gecoder 服务来查找邮政编码,然后检查结果以查看它是否存在。我假设如果你得到一个邮政编码和一个正常的 lat, lng 对,你可以断定邮政编码是真实的。

    下面的代码(诚然使用现已弃用的 V2 API 显示了一种以美国为中心的搜索方法)。优点是最终用户和 Google 计算资源和带宽用于进行验证。

    虽然我发现 Google 的 gecoder 速度快得令人眼花缭乱,但我不知道这对您来说是否有点繁重。

    gecoder = new GClientGeocoder();
    
    geocoder.getLocations(zipcode, function(response) {
        if (response && response.Status.code === 200) {
            var places = response.Placemark;
            for (var p in places) {
                if (places[p].AddressDetails.Country.CountryNameCode === 'US') {
                    // lat => places[p].Point.coordinates[1],
                    // lng => places[p].Point.coordinates[0],
                    // zip => places[p].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber
                }
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多