USA’s Zip Code Validation in PHP

Advertisement

My friend Sushil was trying to write the regular expression for validating the format of the zip code of USA. After spending few minutes, I came to this solution.

The valid format of zip code of USA might be a five digit number like “12345” or in the format of 4 digit followed by first 5 digits like “12345-5434“.And, we need a function to validate the both kind of format of zip code.

Here is the function,

function validateUSAZip($zip_code)
{
  if(preg_match("/^([0-9]{5})(-[0-9]{4})?$/i",$zip_code))
    return true;
  else
    return false;
}

Now, let’s look at the explanation of the regular expression used in this function. A perl regular expression always starts with “/” and ends with “/” and “i” after closing “/” refers that the expression is “case insensitive”.As you know, “^” refers that, it is the beginning of the expression. And, the expression “([0-9]{5})” tells that there should be exactly 5 digits at the beginning of the expression. Furthermore, the second expression “(-[0-9]{4})“, means that after first 5 digit there should be another 4 digit followed by the “-” sign.The “?” sign next to second expression tells that the second expression is optional.And finally, the “$” sign before “/i” refers that it is the end of the expression.

Popularity: 5% [?]

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Date format validation in PHP
» Ip address validation in PHP using regular expression
» USA’s phone number validation using Regular expression in PHP
» Canada’s postal code validation in PHP

10 Comments on “USA’s Zip Code Validation in PHP”

  • Denny wrote on 9 June, 2008, 14:05

    Why not simplify?

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

  • Roshan wrote on 9 June, 2008, 15:44

    ya denny you can surely simply the above function like this…

  • smithveg wrote on 21 July, 2008, 7:07

    Good, Thank for your quote.
    That’s pretty easy to integrate and use.

  • Chris wrote on 3 November, 2008, 15:56

    But I don’t believe 00000 is a valid zip code, nor is 00123. Basically, a zip code can start with a zero, but if it does, the second digit cannot be zero.

  • Tim wrote on 4 June, 2009, 6:17

    Instead of matching the ZIP code using regular expression, you can download the latest US ZIP code database from the following page for free. You can load the list into a database for real time validation and it is the most accurate method.

    http://www.zipcodeworld.com/zipcodefree.htm

  • Mathieu Beausoleil wrote on 19 October, 2009, 18:36

    @Tim: Don’t forget to upgrade your database frequently, new Zip Codes are added each week / month ;-)

  • Cheap Hosting | Flithost.com wrote on 19 April, 2010, 12:22

    Awesome it works fine

  • Jeremiah Johnson wrote on 17 May, 2010, 12:07

    Darn. I was hoping that the zipcode website linked in a previous post (http://www.zipcodeworld.com/zipcodefree.htm) would allow you to create a link directly to the text/CSV file of the database so we could automatically update our databases with a script :(

    However, you are required to fill out a form and can only receive the database file through email.

Trackbacks

  1. PHP Coding School » Blog Archive » php tips [2008-03-20 18:59:04]
  2. What will be validation for Zip code ? - CodeCall Programming Forum

Write a Comment

 


Copyright © 2014 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.