🔍Regex Recipes
ZIP/Postal Code (Generic)
Generic postal code validation with country-specific notes.
Pattern
^[A-Z0-9]{3,10}$Explanation
Very generic pattern accepting 3-10 alphanumeric characters. Country-specific validation recommended.
Examples
US ZIP
Input
90210
Output
✓ Match
US ZIP+4
Input
902101234
Output
✓ Match
UK Postcode
Input
SW1A1AA
Output
✓ Match
Canada
Input
K1A0B1
Output
✓ Match
Code Examples
JavaScript
// Country-specific patterns
const patterns = {
US: /^\d{5}(-\d{4})?$/, // 12345 or 12345-6789
UK: /^[A-Z]{1,2}\d[A-Z\d]?\s*\d[A-Z]{2}$/i, // SW1A 1AA
CA: /^[A-Z]\d[A-Z]\s*\d[A-Z]\d$/i, // K1A 0B1
DE: /^\d{5}$/, // 12345
FR: /^\d{5}$/, // 75001
RS: /^\d{5}$/ // 11000
};
function validatePostal(code, country) {
const pattern = patterns[country];
return pattern ? pattern.test(code) : /^[A-Z0-9]{3,10}$/.test(code);
}Try it Now
💡 Tips
- Use country-specific patterns when possible
- UK postcodes have spaces (SW1A 1AA)
- Canadian format: A1A 1A1
- Consider address validation API for production
⚠️ Common Pitfalls
- Postal formats vary greatly by country
- Some countries don't use postal codes
- Spaces and dashes vary by format
- Generic pattern is very permissive