Skip to content Skip to sidebar Skip to footer

Zipcode Validation For US

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}))?$/

Solution 1:

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

Solution 2:

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

}


Solution 3:

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.


Solution 4:

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


Post a Comment for "Zipcode Validation For US"