CEP Range Validator
Identify Brazilian region from first 2 digits of CEP code.
The legacy 5-digit CEP and the 1992 migration to 8 digits
The CEP (Codigo de Enderecamento Postal) is the Brazilian postal code maintained by Correios. Created in 1971 as a 5-digit code covering broad regions, it was redesigned in 1992 to the modern 8-digit format XXXXX-XXX in order to encode street-level granularity in large urban centers. This tool is dedicated to the diagnosis and conversion problem that still appears in legacy ERPs, scanned documents and historical databases: how to handle CEPs in the old, shorter format and how (or whether) to lift them to the current 8-digit standard.
The migration was not a trivial padding operation. The first 5 digits of the new format are not identical to the old code in every case: many municipal centers were re-coded to free numeric ranges for new neighborhoods, post offices and grandes usuarios. Correios published a tabela de equivalencia at the time, but it is now only available inside the DNE (Diretorio Nacional de Enderecos) commercial product, not in the free ViaCEP/BrasilAPI lookups.
Detection and validation rules
A robust legacy CEP validator must distinguish three cases and apply different rules to each:
- Exactly 5 digits (
^\d{5}$): legacy pre-1992 format. Valid for historical reference only. - Exactly 7 digits: usually a typo or stripped leading zero from an 8-digit CEP. The tool flags it as likely invalid and suggests prefixing a zero.
- Exactly 8 digits (
^\d{5}-?\d{3}$): current standard. Valid format.
For pure format validation (without DNE lookup), this regex is sufficient: ^\d{5}-?\d{3}$. To capture the legacy case explicitly: ^(\d{5})(-?\d{3})?$, where the second group is optional. Any modern web form that still accepts 5 digits as valid is an anti-pattern โ Correios does not deliver mail keyed only by the legacy code anymore.
Converting legacy to current: when it is and is not possible
A simplistic padding rule ("append 000 to the 5-digit code") works only for some cities and only when the address is unambiguous at the municipality level. For capitals and metropolitan areas, the conversion is one-to-many: a single 5-digit code may map to dozens of new 8-digit CEPs, one per street or block. There is no algorithm that recovers the lost information; you need either:
- A query to the Correios DNE historical table (paid, available on contract).
- The original address (street name, neighborhood, city) to re-geocode against a current CEP lookup like ViaCEP.
- For small cities that still use a single 8-digit CEP (called CEP geral, ending in -000), the legacy 5-digit code plus the standard
-000suffix may approximate the modern code, but this is not guaranteed.
In short: never assume 12345 equals 12345-000 in production. Treat the conversion as a data-quality problem and not as a string transformation.
Modern CEP structure and special ranges
The 8-digit CEP is split into 5 digits of geographic prefix + 3 digits of distribution suffix. The geographic prefix itself encodes region, sub-region, sector, sub-sector and division, decreasing in geographic scope from left to right. The 3-digit suffix encodes the specific block, building or grande usuario. Special suffixes worth knowing:
- -000: generic CEP for the whole city (small municipalities).
- -899 to -900: caixas postais and grandes usuarios reserved range.
- -970: typically a Correios agency or post office itself.
- -999: caixa postal community in larger cities.
The first 5-digit region prefixes used today still echo the 1971 division: 0xxxx Greater Sao Paulo, 1xxxx interior SP, 2xxxx RJ + ES, 3xxxx MG, 4xxxx BA + SE, 5xxxx PE + AL + PB + RN, 6xxxx CE + PI + MA + Norte, 7xxxx DF + Centro-Oeste, 8xxxx PR + SC, 9xxxx RS.
Practical use cases for a legacy CEP validator
Where this tool actually helps in real Brazilian software projects:
- ERP migration: lifting customer addresses from systems built before 1992 (cobol mainframes still in use at some banks and utilities).
- Historical document parsing: OCR over scanned contracts, public records, old land titles where addresses use the legacy CEP.
- Genealogy and academic research: cross-referencing legacy CEPs with current IBGE data.
- Data quality auditing: spotting fields that still hold 5 or 7-digit values in modern databases โ usually a sign of import bugs.
- Customer support: explaining to a user why their typed CEP does not lookup on ViaCEP.
For new development, always validate against the 8-digit standard. ViaCEP (viacep.com.br/ws/{CEP}/json/) and BrasilAPI (brasilapi.com.br/api/cep/v2/{CEP}) are free, public APIs that resolve 8-digit codes to full addresses. Both have monthly synchronizations with the Correios DNE and rate limits around 10,000 requests per day for free tier.
FAQ
Does Correios still accept 5-digit CEPs on envelopes?
No. Since the early 1990s the official mail sorting machines require the full 8-digit format. Envelopes with only 5 digits are processed manually and frequently delayed or returned. Mailing services (NF-e, e-commerce) reject 5-digit CEPs at the API level.
Can I just append three zeros to convert a 5-digit CEP?
Only as a last-resort fallback for very small cities that have a CEP geral ending in -000. For capitals and metropolitan areas, the suffix encodes a specific street or block and cannot be inferred. The safe approach is to re-geocode using the original street name through ViaCEP.
Is the 8-digit format mandatory today?
Yes. Every layout that includes a CEP field (NF-e, eSocial, ERP, e-commerce checkout) expects 8 digits. Many ERPs apply the mask XXXXX-XXX automatically and reject anything shorter at the input level.
What is the DNE and how do I access it?
DNE (Diretorio Nacional de Enderecos) is the official, complete database of Brazilian addresses maintained by Correios. It includes the historical conversion tables. It is sold via subscription to logistics companies, banks and e-commerce platforms. Smaller projects use the free ViaCEP/BrasilAPI wrappers, which cover all current 8-digit CEPs but not the full legacy conversion data.
Why does my legacy database show 7-digit CEPs?
Almost always a bug in the import: a column typed as integer dropped the leading zero of an 8-digit CEP. The fix is to left-pad with zeros to 8 digits and re-validate. This tool flags 7-digit inputs and suggests the prefix-zero correction.
Related Tools
CPF Validator
Validate Brazilian CPF numbers instantly using the official algorithm. Useful for testing document validation in applications. No data sent to servers.
Batch CPF Validator
Validate a list of CPFs (one per line) and see which are valid and which are not. No data sent to servers.
Batch CNPJ Validator
Validate a list of CNPJs (one per line) with a summary of valid, invalid and total. No data sent to servers.