Free · No sign-up · Nothing is sent
Spanish DNI and NIE letter: calculate and validate
Type just the number to get the letter, or the full document to check it.
A matching letter means there are no typos, not that the document exists or who it belongs to.
Type the number to get its control letter, or the full document to check it. It detects whether it is a DNI, an NIE (foreigners' ID number) or a K, L or M NIF.
Runs in your browser, even offline. Your ID number is never sent or stored.
What it checks
What the letter checks (and what it does not)
- Yes: whether the letter matches the number. That catches almost every typo.
- No: whether the document exists, is valid or who it belongs to. Only the authorities know that.
How it works
How the DNI letter is calculated
This is the algorithm published by Spain's Ministry of the Interior, based on article 12 of Royal Decree 255/2025. Using their own example, 12345678:
- 101
Take the number
12345678
The 8 digits. Shorter numbers are padded with leading zeros.
- 202
Divide by 23
12345678 ÷ 23 = 536768.6…
Only the remainder matters.
- 303
Keep the remainder
12345678 mod 23 = 14
It is always between 0 and 22.
- 404
Look it up
14 → Z
Remainder 14 is Z. The full DNI is
12345678Z.
DNI letter table (0 to 22)
| Remainder | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Letter | T | R | W | A | G | M | Y | F | P | D | X | B | N | J | Z | S | Q | V | H | L | C | K | E |
Why there is no I, Ñ, O or U
The table has 23 letters and skips I, Ñ, O and U. There is no official explanation. The most common one is that I and O look like 1 and 0, and that 23, a prime number, makes the remainder better at catching single-digit errors.
NIE
How the NIE letter is calculated (X, Y, Z)
The NIE (foreigners' identity number) starts with X, Y or Z, followed by 7 digits and a control letter. To calculate the letter, replace X with 0, Y with 1 and Z with 2, then apply the DNI method:
| NIE | Number used | Remainder | Letter |
|---|---|---|---|
| X1234567 | 01234567 | 19 | L |
| Y1234567 | 11234567 | 10 | X |
| Z1234567 | 21234567 | 1 | R |
K, L and M NIFs
NIFs starting with K, L or M
Tax numbers for people without a DNI or NIE, under Royal Decree 1065/2007, articles 19 and 20:
| Starts with | Who has it |
|---|---|
| K | Spaniards living in Spain under 14 years old (no DNI). |
| L | Spaniards living abroad without a DNI. |
| M | Foreigners without a foreigner identity number (NIE). |
The control letter is calculated the same way, using only the 7 digits.
Differences
DNI, NIF, NIE and TIE: what is the difference?
| Term | What it is |
|---|---|
| DNI | The Spanish national identity card. |
| NIF | The tax number. For a Spaniard with a DNI it is the same number and letter. |
| NIE | The identity number for foreigners: X, Y or Z, 7 digits and a letter. It also works as their NIF. |
| TIE | The foreigner identity card, the physical document with the NIE printed on it. |
Support number
Support number (IDESP) is not your DNI number
Some services ask for the support number, also called IDESP. It is the number of the physical card, changes every time you renew it and has no control letter. It is printed on the front of the card (under «IDESP», «NUM SOPORT» or «NUM SOPORTE», depending on the model).
Code
Calculate or validate the letter in your own code
Same algorithm this tool uses, no libraries:
// DNI or NIE control letter (Spanish Ministry of the Interior algorithm).
const LETTERS = "TRWAGMYFPDXBNJZSQVHLCKE";
function dniLetter(document) {
const clean = document.toUpperCase().replace(/[\s.-]/g, "");
// NIE: X → 0, Y → 1, Z → 2
const number = clean.replace(/^[XYZ]/, (l) => "XYZ".indexOf(l));
return LETTERS[Number(number.slice(0, 8)) % 23];
}
function isValidDni(document) {
const d = document.toUpperCase().replace(/[\s.-]/g, "");
if (!/^(\d{8}|[XYZ]\d{7})[A-Z]$/.test(d)) return false;
return dniLetter(d.slice(0, -1)) === d.at(-1);
}
dniLetter("12345678"); // "Z"
isValidDni("X1234567L"); // trueLETTERS = "TRWAGMYFPDXBNJZSQVHLCKE"
def dni_letter(number: str) -> str:
number = number.upper().replace(" ", "").replace("-", "")
number = number.replace("X", "0", 1).replace("Y", "1", 1).replace("Z", "2", 1)
return LETTERS[int(number[:8]) % 23]
dni_letter("12345678") # "Z"Onboarding customers with direct debit? Check their bank account too with the IBAN validator.
FAQ
DNI letter frequently asked questions
How is the DNI letter calculated?
Divide the number by 23 and look up the remainder in TRWAGMYFPDXBNJZSQVHLCKE. Remainder 0 is T and 22 is E.
What does the DNI letter mean?
Nothing by itself: it is a control character derived from the number to catch typos.
How is the NIE letter calculated?
Like the DNI one, replacing the first letter with a digit: X with 0, Y with 1 and Z with 2.
Are the DNI and the NIF the same?
For a Spaniard with a DNI, the NIF is the same number and letter. The DNI is the card; the NIF is the tax number.
Does a correct letter mean the ID is real?
No. It only means the number and letter match. Whether it exists and who owns it is known only to the authorities.
Do you store the number I type?
No. It is calculated in your browser and never leaves your device.
About
About this validator
Built by Black Wings, a software studio in Valencia (Spain) working with small and medium businesses. Our validators are free, single-purpose tools that show how we work.
- Algorithm: Spanish Ministry of the Interior for DNI and NIE.
- Regulations: Royal Decree 255/2025 (DNI), Royal Decree 1065/2007 (NIF, K, L, M) and Order INT/2058/2008 (NIE).
- Code: written by us in TypeScript, no external libraries, with automated tests.
- Reviewed: 22 November 2026.
Privacy
What happens to your ID number
Your number never leaves your browser. It is calculated with JavaScript on your device: not sent to any server, not stored, not recorded in any analytics.
Try it: load the page, switch off your internet and it keeps working. The site measures visits (Google cookies only if you accept them), but what you type is never sent. More in our privacy policy.
Other validators
More free validators
Need this inside your own software?
Validating IBANs and tax IDs when a customer signs up, checking whole lists or connecting invoicing to the bank: that is the kind of work we do at Black Wings.
Tell us about it