JavaScript zip code validation
Zip code is the postal code used by the United States Postal Service (USPS). The basic format consists of 5 digits. Later, an extended code was introduced that contains a hyphen and 4 more digits. Some valid Zip Codes: 20521-9000 42223 85254 Here is a simple function to validate a ZIP code: function isUSAZipCode(str) { return /^\d{5}(-\d{4})?$/.test(str); } This function uses a regular expression that checks for 5 digits ( \d{5} ) followed by optional hyphen and four digits( (-\d{4})?