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) {
    let 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);
}

Posted: March 20, 2023

Return to the snippets listing