zip code validation blogIn a previous post we explained simple regular expression phone number validation; in this post, we will go over regular expression ZIP code validation.

Online forms are becoming more and more common on all types of websites for gathering information about website users. For shipping forms, billing forms and other types of contact forms, the ZIP code or postal code box is often a required field. But in order to ensure that the data entered into the ZIP code box is a ZIP code, the field must be validated.

In the United States, the standard ZIP code is five digits; however, sometimes an additional four digits are added after a dash to help sort mail even more precisely. Keep in mind, not all website users may know their nine digit ZIP code, so it is often better to only require the five digit ZIP code.

If your website users are primarily from the United States, use the following regular expression ZIP code validation:


  • ^          Start of the string
  • \d{5}  Match 5 digits
  • -\d{4}Match a hyphen then 4 digits
  • ?          The pattern in ( ) is optional
  • $          End of the string

But what about international ZIP codes? Not all countries have postal codes; Ireland, Hong Kong and Panama are a few countries which do not. Some countries use letters as well as numbers in their ZIP codes. Some use spaces or other characters like dashes. If your website audience is comprised of users from around the world, you may want to consider how you format and validate the ZIP code field. In addition, you may want to make this field optional so that your form does not give users from countries without ZIP codes an error when the field is left blank and prohibit them from proceeding with their submission.

For more information on international ZIP code validation, please contact us or see the following post by our parent company Unidev.

Tags: , , , , , ,

Leave your comment