Spanish DNI & NIE Validator

Validate Spanish DNI and NIE numbers in bulk — format and check letter.

DNIs are validated in your browser — never sent anywhere

Check the format and mod-23 letter — one DNI/NIE or a whole list

Example loaded — edit it or clear it

Total
Valid
Invalid

What this validator checks — and what it cannot confirm

The DNI (Documento Nacional de Identidad) is the identity number assigned to every Spanish national, and the NIE (Número de Identidad de Extranjero) is its equivalent for foreign residents. Both appear on tax forms, bank contracts, invoices, and every formal procedure in Spain, so entering them wrong has real consequences — from rejected filings to mismatched records. This validator checks one DNI or NIE, or hundreds at a time, and tells you exactly what is wrong with each.

The DNI format is 8 digits followed by a single check letter. The NIE format is a leading letter (X, Y, or Z) followed by 7 digits and a check letter. Both use the same algorithm: divide the numeric part by 23 and look up the remainder in a fixed 23-letter table — T, R, W, A, G, M, Y, F, P, D, X, B, N, J, Z, S, Q, V, H, L, C, K, E. For NIEs, the leading letter is first replaced by a number: X becomes 0, Y becomes 1, Z becomes 2.

The algorithm is deliberately simple and deterministic — there are no weights, no sums, just a division and a lookup. But it catches typos effectively: changing any single digit in the body changes the remainder, and 22 times out of 23 it changes the expected letter. Transposing two adjacent digits also changes the remainder in most cases.

What no outside tool can confirm is whether the DNI or NIE is actually issued by the DGP (Dirección General de la Policía). A structurally valid number can be unassigned or expired. Use this validator as the bulk first filter for cleaning data, and the official channels for final verification. Everything runs in your browser — the numbers you paste are never sent anywhere.

DNI and NIE structure

TypeFormatExampleAlgorithm
DNI (Spanish nationals)8 digits + 1 letter12345678Znumber mod 23 → letter lookup
NIE (foreign residents, X series)X + 7 digits + 1 letterX1234567LReplace X with 0, then mod 23
NIE (Y series)Y + 7 digits + 1 letterY1234567XReplace Y with 1, then mod 23
NIE (Z series)Z + 7 digits + 1 letterZ1234567RReplace Z with 2, then mod 23

The X series was the original NIE series and covers the majority of existing NIEs. The Y series began after the X series was exhausted. The Z series is reserved for future use. The check letter is always one of 23 possible letters — note that I, Ñ, O, and U are excluded from the table to avoid visual confusion with 1, Ñ, 0, and V.

The mod-23 check letter, step by step

The check letter is the simplest of all national ID algorithms: divide the number by 23, take the remainder (0 to 22), and look it up in this table:

Remainder → Letter
 0  T     6  Y    12  N    18  H
 1  R     7  F    13  J    19  L
 2  W     8  P    14  Z    20  C
 3  A     9  D    15  S    21  K
 4  G    10  X    16  Q    22  E
 5  M    11  B    17  V
The complete lookup table. Letters I, Ñ, O, and U are deliberately excluded.
DNI: 12345678
12345678 mod 23 = 14  →  table[14] = Z
→ 12345678Z

NIE: X1234567
Replace X → 0  →  01234567 = 1234567
1234567 mod 23 = 19  →  table[19] = L
→ X1234567L
Worked examples for a DNI and a NIE.

Validating DNI/NIE in Excel, Google Sheets, and SQL

To quickly check a DNI column for format errors, these formulas catch anything that does not match the expected pattern. The mod-23 letter itself requires a lookup formula or code.

Google Sheets (A2 = DNI/NIE):
DNI: =REGEXMATCH(A2, "^[0-9]{8}[A-Z]$")
NIE: =REGEXMATCH(A2, "^[XYZ][0-9]{7}[A-Z]$")
Both: =REGEXMATCH(A2, "^([0-9]{8}|[XYZ][0-9]{7})[A-Z]$")

Excel 365:
=REGEXTEST(A2, "^([0-9]{8}|[XYZ][0-9]{7})[A-Z]$")
Format check — catches wrong lengths and non-alphanumeric characters, but does not verify the mod-23 letter.
-- PostgreSQL: find DNIs/NIEs with bad format
SELECT dni FROM personas
WHERE dni !~ '^([0-9]{8}|[XYZ][0-9]{7})[A-Z]$';

-- MySQL 8
SELECT dni FROM personas
WHERE dni NOT REGEXP '^([0-9]{8}|[XYZ][0-9]{7})[A-Z]$';
SQL queries to find invalid-format DNIs/NIEs. The mod-23 letter check requires code or a stored function — validate it here in bulk.

FAQ

Does this validator confirm the DNI is issued?

No — and no outside tool can. This checks the format and the mod-23 check letter, which is enough to catch typos. To confirm a DNI or NIE has actually been issued, you need the official channels (DGP / police). The efficient workflow is to bulk-clean here first, then verify officially only the entries that passed.

What is the difference between DNI and NIE?

The DNI is for Spanish nationals (8 digits + letter). The NIE is for foreign residents and starts with X, Y, or Z followed by 7 digits and a letter. Both use the exact same mod-23 algorithm for the check letter — the NIE just replaces its leading letter with 0, 1, or 2 before computing.

Is it safe to paste real DNI and NIE numbers?

Yes. Validation runs entirely in your browser with JavaScript: the numbers are never sent to any server, there are no accounts or logging, and you can prove it by disconnecting from the internet after the page loads — the tool keeps working.

Related tools