美国的邮政编码验证


Zipcode validation for US

谁能告诉我如何使用正则表达式验证美国的邮政编码。

我使用以下表达式进行验证

var us_postcode_regular = /^([0-9]{5})(?:[-'s]*([0-9]{4}))?$/;

但是,它无法验证所有 00000 或 11111 或类似数字。

以及我如何验证不正确的邮政编码,例如不属于美国邮政编码的五位数字。

if(ctype_digit($zip) && strlen($zip) == 5){ echo 'US Zip Code'; }

http://www.zipcodeapi.com/API#zipToLoc 使这变得非常简单。 调用如下所示:

http://www.zipcodeapi.com/rest/<api_key>/info.<format>/<zip_code>/<units>

错误的邮政编码会抛出您可以捕获的错误。有效的邮政编码将返回JSON(或XML或CSV)响应,如下所示:

{
"zip_code": "08057",
"lat": 56.595452,
"lng": -69.784543,
"city": "Classified",
"state": "NJ",
"timezone": {
    "timezone_identifier": "America/New_York",
    "timezone_abbr": "EDT",
    "utc_offset_sec": -14400,
    "is_dst": "T"
},
"acceptable_city_names": [
    {
        "city": "Classified",
        "state": "NJ"
    },
    {
        "city": "Classified",
        "state": "NJ"
    }
]

}

我相信

,你可以使用的最简单的是这样的:

function validateUSAZip($zip_code) {
return preg_match(“/^([0-9]{5})(-[0-9]{4})?$/i”,$zip_code);
}

它实际上使用与您类似的正则表达式。

<!-- Hi developers this is a simple demo for **zip code validation for us country** And this code is very usefull for you. -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Validate US Zip Code in JavaScript</title>
    <script type="text/javascript">
        function IsValidZipCode(zip) {
            var isValid = /^[0-9]{5}(?:-[0-9]{4})?$/.test(zip);
            if (isValid)
                alert('Valid ZipCode');
            else {
                alert('Invalid ZipCode');
            }
        }
    </script>
</head>
<body> 
<form>
<input id="txtZip" name="zip" type="text" /><br />
<input id="Button1" type="submit" value="Validate"onclick="IsValidZipCode(this.form.zip.value)" />
</form>
</body>
</html>

欲了解更多信息。http://www.devcurry.com/2010/07/validate-us-zip-code-using-javascript.html