Validate Country, State, and Zip Codes
This article outlines best practices for validating country, state, and ZIP codes using the lookup file to ensure the input info is valid and correctly formatted.
The lookup file
The lookup file provides a comprehensive list of valid codes for countries, states, and postal or ZIP codes. It includes two-letter ISO codes for countries, valid state or province codes for each country, and valid postal or ZIP codes for each region or locality.
Country and state ISO codes are typically represented by two letters (such as "US" for the United States, and "CA" for California), while ZIP codes vary in format depending on the country.
Socure's lookup file is a CSV file that contains the following columns:
- countryCode: The two-letter ISO 3166-1 country code.
- countryName: The country's full name.
- stateCode: The two-letter state or province code (left blank for countries without states or provinces).
- stateName: The state's or province's full name (left blank for countries without states or provinces).
- zipCodeFormat: The ZIP code format used in the country (left blank for countries without postal codes).
- zipCodeRegex: The regular expression to validate ZIP codes (left blank for countries without postal codes).
Below is the file's snippet:
Contact your Account Manager to access Socure's lookup file.
Note:
When comparing the input with the ZIP code format:
- A = must be a letter
- N = must be a number
Validate a code using the lookup file
To validate a country, state, or ZIP code, compare the input code with the data in the file:
- For country codes: check if the input matches any code listed in the countryCode column. If a match is found, the input is a valid country code.
- For state codes: check if the input matches any code listed in the stateCode column. If a match is found, the input is a valid state code.
- For postal or ZIP codes: check if the input matches the format listed in the zipCodeFormat and zipCodeRegex columns for the matched country.
Below are example inputs and the expected validation results:
| Input | Validation Result | Reason |
|---|---|---|
| CA | Valid | CA is a valid state code. |
| US | Valid | US is a valid country code. |
| V3B6K9 | Valid | The input matches the ZIP code format ANA NAN and ZIP code regex ^[A-Za-z]\d[A-Za-z]\s\d[A-Za-z]\d$ for Canadian ZIP codes. |
| 5200B, FR | Invalid | The input does not match the ZIP code format NNNNN for French ZIP codes. |
Updated about 2 months ago
