4

Can any one tell me how can i validate zipcode for US using regular expression.

I used below expression for validation

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

However it is not able to validate all 00000 or 11111 or similar numbers.

and how can i validate the incorrect zipcode like the five digits which are not the part of US zipcode.

Satyendra Mishra
  • 523
  • 2
  • 9
  • 21
  • It's [**working for me.**](http://regexr.com?36itk) – theftprevention Oct 02 '13 at 19:12
  • 2
    That pattern is already correct. You cannot really determine valid zip codes from a regular expression; you can only tell if it's in the right form (5 or 9 digits). – Pointy Oct 02 '13 at 19:15
  • 3
    If you want to make sure that the zip code exists, check out USPS's Address Validation API: https://www.usps.com/business/web-tools-apis/address-information.htm – aynber Oct 02 '13 at 19:25
  • 1
    What is wrong with doing this: `if(ctype_digit($zip) && strlen($zip) == 5){ echo 'US Zip Code'; }` – MonkeyZeus Oct 02 '13 at 20:10
  • Great start to ensuring your data quality Satyendra - just watch out as not everyone has a 5 digit zip code! http://stackoverflow.com/questions/22620156/making-sure-that-a-user-cant-submit-less-than-5-numbers-for-zip-code-in-a-javas/22631342#22631342 Al. – Al Mills Apr 02 '14 at 20:52
  • Try this discussion http://stackoverflow.com/questions/160550/zip-code-us-postal-code-validation – Ernesto Rendon Jun 09 '15 at 18:22

4 Answers4

1
if(ctype_digit($zip) && strlen($zip) == 5){ echo 'US Zip Code'; }
Satyendra Mishra
  • 523
  • 2
  • 9
  • 21
0

http://www.zipcodeapi.com/API#zipToLoc makes this very simple. The call looks like:

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

Bad zipcodes will throw an error that you can catch. A valid zipcode will return a JSON (or XML or CSV) response look like:

{
"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"
    }
]

}

Bob Bickel
  • 349
  • 2
  • 3
0

I belive, the simplest one you can use is this:

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

It is actually using similar regex like yours.

-1
<!-- 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>

For more info. http://www.devcurry.com/2010/07/validate-us-zip-code-using-javascript.html

Yagnesh bhalala
  • 1,107
  • 1
  • 15
  • 17