Zip and Postal Code Validation

Description: This code validates both 5 and 9 digit zip codes along with Canadian postal codes with and without a space. It is also case insensitive. It returns true if it is valid and false if it isn't. Does not check if they actually exist however.
Tested Platform: All Modern Browsers
Language: JavaScript
// Checks US 5 digit and 9 digit zip codes as well as Canadian postal code (capital insensitive)

function checkPostal(postalCode) {
   var pattern = new RegExp("(^\\d{5}(-\\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[A-Z]{1} *\\d{1}[A-Z]{1}\\d{1}$)", "i");
   return pattern.test(postalCode);
}

Submitted: March 05, 2013

Return to the snippets listing