RG Validator
Validate Brazilian RG numbers in the SP format (9 digits + check digit). Browser-side verification, no data sent to servers.
How does SP RG validation work?
A São Paulo RG carries 8 base digits and 1 check digit. To reach that check digit, you add up the 8 digits weighted by [2,3,4,5,6,7,8,9] and take the result modulo 11. A remainder of 0 means the check digit is 0; a remainder of 1 becomes X; in any other case the check digit is 11 minus the remainder.
All of it is processed inside your own browser.
RG validation: the trap of a non-national algorithm
The RG (Registro Geral) is the Brazilian state-issued ID card. Unlike CPF, CNPJ or RENAVAM, the RG has no nationwide check-digit algorithm. Each SSP (Secretaria de Seguranca Publica) of the 27 federation units defines its own format, its own digit count and its own validation rule — or no rule at all. Any tool that claims to validate "RG" universally is, at best, validating the SP rule and silently rejecting valid documents from other states.
This page focuses on what a validator can really do: enforce per-state syntax, run the SP modulo 11 check, sanitize input, integrate with form libraries and flag combinations that look suspicious. For a synthetic RG with a valid SP check digit, see the companion generator.
The SP RG algorithm (the only published one in wide use)
The SSP/SP rule is the most documented and the only one most libraries actually implement. The number has 8 digits plus a check digit, displayed as XX.XXX.XXX-D. The check digit may be 0-9 or X (Roman 10).
- Multiply the first 8 digits by weights 2, 3, 4, 5, 6, 7, 8, 9.
- Sum the products.
resto = soma mod 11.- If
resto == 10, DV = X; otherwise DV =resto.
function validateRgSP(value) {
const raw = String(value).replace(/[^0-9X]/gi, '').toUpperCase();
if (raw.length !== 9) return false;
const digits = raw.slice(0, 8);
const dv = raw[8];
if (!/^\d{8}$/.test(digits)) return false;
const weights = [2, 3, 4, 5, 6, 7, 8, 9];
const sum = weights.reduce((a, w, i) => a + Number(digits[i]) * w, 0);
const r = sum % 11;
const expected = r === 10 ? 'X' : String(r);
return expected === dv;
}
Other states: what changes
- RJ (Detran/IFP): usually 9 digits with a numeric DV; algorithm not published officially, so libraries fall back to format-only validation.
- MG: format
MG-XX.XXX.XXXwith letters as state prefix; no public DV formula. - RS, BA, PE, PR, SC: vary between 8 and 10 digits; some use a sequential numeric DV without modulo 11.
- DF: 8 digits issued by Policia Civil/DF; DV rule not published.
- Federal Police (RNE): foreigners hold an RNE (now CRNM) with its own alphanumeric pattern.
In practice, a multi-state RG validator should at most: (i) strip punctuation, (ii) check digit count against the issuing UF, (iii) run the SP modulo 11 when issuer is SP and (iv) reject obvious patterns like 00000000-0.
Pre-validation regex and masks
// Accept punctuation, but treat X as valid only at the last position
const RG_SP_RE = /^\d{1,2}\.?\d{3}\.?\d{3}-?[0-9Xx]$/;
const clean = input.replace(/[^0-9Xx]/g, '').toUpperCase();
Be careful: the input field should accept both the masked form 12.345.678-X and the bare form 12345678X. Strip dots, hyphens and whitespace before applying the algorithm. Never lowercase after the comparison — uppercase the input first.
JavaScript, Python and Laravel libraries
- brazilian-values (npm): provides
isRG()but documents the SP-only scope explicitly. - brazilian-utils (npm): a generic format check, not a per-state DV.
- validador-br (PyPI): exposes
rgwith SP rule andsomente_formatomode. - geekcom/validator-docs (Laravel): rule
'rg'validates SP only by default.
CIN: the unified national ID replacing RG
Created by Law 13.444/2017 and rolled out progressively since 2023, the Carteira de Identidade Nacional (CIN) reuses the CPF as the unique national number. The card is polycarbonate, includes ICP-Brasil digital signature and a QR Code for offline verification. The government plan replaces all 27 state RGs with the CIN by 2032. After full migration, "RG validation" becomes "CPF validation plus signature check" — much simpler and finally uniform.
Anti-fraud: triple-cross with CPF and date of birth
A solo RG is a weak proof of identity, especially because Brazilians can legally hold multiple RGs (one per state) — a historical anomaly. Solid onboarding flows cross-reference at least three fields:
- RG number with issuing UF and SSP-orgao expedidor.
- CPF validated by its own modulo 11.
- Date of birth and full name to query Serasa, Boa Vista or Quod.
Modern KYC vendors (Idwall, Unico, Stripe Identity Brasil, AcessoBio) increasingly drop RG altogether and rely on CPF plus facial biometrics, expecting full CIN rollout within five years.
No official government lookup API
There is no public RG lookup API. Each state SSP runs a closed system that only the police and partner agencies can query. Even CIN, despite digital signatures, has no open verification endpoint — the QR Code is read by a TSE-developed app that calls a restricted gov.br backend. This is structurally different from CPF (which has a free RFB consultation) and means RG validation will always remain client-side syntactic checking, never authoritative.
FAQ
Is there a single national RG algorithm? No. Only SP has a publicly documented modulo 11 rule. Other states do not publish their DV formula.
Why does my RG end with X? X stands for 10 in the SP modulo 11 algorithm. About 9% of SP RGs end in X.
Will CIN replace RG? Yes, gradually until 2032. New issuances default to CIN, but existing RGs remain valid throughout the transition.
Can I validate an RG online via government API? No. There is no public endpoint. Any service that "verifies an RG" is checking format only, or buying data from a private bureau.
Does the RG expire? Legally no, but banks and airports reject documents whose photo is more than 10 years old. CIN introduces a 10-year recommended renewal.
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.
Validate an RG number (SP format)
In the São Paulo format, the RG has nine digits, and the last one is a check digit worked out from the others. This validator redoes that calculation and tells you whether the number is well formed, which helps catch typos in registrations.
It's handy for checking a form, validating a spreadsheet, or cleaning up a database that holds São Paulo RGs. Just keep in mind that the RG has no single national standard like the CPF does, each state runs its own. So this validation applies specifically to the SP format.
The check happens entirely in the browser, with nothing sent to servers. You can look over real documents without worry, since what you type stays on your device.