This module provides a function to validate postal codes and ZIP codes for various countries. It has no user interface of its own, but can be used by other modules.
The main function is postal_code_validation_validate(string $postal_code, string $country)
. $postal_code is the postal code or ZIP code that you are validating. $country is the country that the code is from. The country can be omitted; if it is not there, the function will try to figure out what country the postal code is from.
The function returns an array with these elements:
- country
- Country code of the postal code.
- country_supported
- boolean, whether the country is supported.
- province
- The province or province code if it can be determined.
- postal_code
- The formatted postal code.
- postal_code_changed
- boolean, whether postal_code is different from the value sent in for validation.
- error
- An error message if the code is invalid.
Each is set to FALSE if a valid response is not possible.
In some cases, the country returned will be different from the one sent in. For example, if you search for a ZIP code with country "US", but that ZIP code is located in the US Virgin Islands, the country returned will be "VI".
Countries are always specified with the ISO 3166-1 alpha-2 country code, such as US, GB, and CA.
The module also provides the function postal_code_validation_get_dependencies()
which returns an array of countries which use the postal code system of another country.
Project information
- Module categories: Site Structure
- 1,250 sites report using this module
- Created by Liam Morland on , updated
- Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.