How the DNI/NIE check letter is calculated
The check letter validates a Spanish DNI or NIE number using a fixed algorithm: divide the 8-digit number by 23, and use the remainder to pick a letter from the string 'TRWAGMYFPDXBNJZSQVHLCKE' (0-indexed). For a NIE, the leading letter is first replaced with a digit — X becomes 0, Y becomes 1, Z becomes 2 — before applying the same calculation. This lets you both generate the correct letter for a new number and confirm that an existing DNI/NIE is not mistyped.
How do I calculate the DNI/NIE check letter?
To calculate the check letter, take the 8-digit number (for a NIE, first replace the leading letter with a digit: X=0, Y=1, Z=2), divide it by 23, and use the remainder as an index into the fixed string 'TRWAGMYFPDXBNJZSQVHLCKE'. Example: 12345678 mod 23 = 15, and the letter at index 15 is 'Z'.
Steps to calculate or validate
- If the input starts with X, Y or Z (a NIE), replace that letter with 0, 1 or 2 respectively to form an 8-digit number.
- Take the resulting 8-digit number as-is for a DNI.
- Divide the 8-digit number by 23 and keep the remainder (0-22).
- Look up that remainder as a position in the string 'TRWAGMYFPDXBNJZSQVHLCKE' (position 0 = T) to get the check letter.
- If a letter was already provided, compare it to the calculated one to determine validity.
Formula
Letter = "TRWAGMYFPDXBNJZSQVHLCKE"[number mod 23]
- number = the 8-digit DNI number, or the NIE number with X/Y/Z replaced by 0/1/2
- mod 23 = the remainder after dividing by 23, always between 0 and 22
- the 23-letter reference string skips vowels and letters easily confused with digits, by official design
Example DNI numbers and their letters
| Number | mod 23 | Letter |
|---|
| 12345678 | 15 | Z |
| 00000000 | 0 | T |
| 87654321 | 21 | X |
| 25000000 | 13 | N |
| 99999999 | 1 | R |
Frequently asked questions
What is the difference between DNI and NIE?
DNI (Documento Nacional de Identidad) is issued to Spanish citizens and is always 8 digits plus a letter. NIE (Número de Identidad de Extranjero) is issued to foreign residents and starts with X, Y or Z followed by 7 digits and a letter.
Why does a NIE replace its letter with a digit before calculating?
The check-letter algorithm was designed for an 8-digit number. Since a NIE only has 7 digits after its leading letter, that letter is mapped to a digit (X=0, Y=1, Z=2) to complete the 8-digit number before applying the same modulo-23 formula as a DNI.
Can this tool detect a fake or made-up DNI number?
It can confirm the check letter is mathematically consistent with the 8 digits, which catches typos and randomly invented numbers. It cannot confirm the number was actually issued by the Spanish authorities — that requires an official registry lookup.
Why do letters like I, O and U never appear?
The reference string 'TRWAGMYFPDXBNJZSQVHLCKE' intentionally excludes vowels and visually similar letters/digits to reduce transcription errors when the ID is read aloud or copied by hand.
This tool implements the public check-letter algorithm only; it does not verify that a DNI/NIE was actually issued, is currently valid, or belongs to a real person. Use an official government service for identity verification.