Spanish NIE generator and validator

Generate and validate a Spanish NIE for tests, with the check digit algorithm shown with a worked example.

loading Spanish NIE generator and validator
About this tool

Spanish NIE: what it is and how to test it

The NIE (Número de Identidad de Extranjero) is the identification number of foreigners in Spain, used for taxes, work, rent and bank accounts. It looks like the DNI with a letter in front, and the control letter is computed the same way after the first letter is turned into a digit.

For tests only. These numbers have a correct format and check digit but belong to no real person or account. Never use them to deceive, to open accounts or to bypass a check. The numbers on this page come from the same generator as the tool, with a fixed seed so that the examples do not change; the tool creates new ones at every click.

Structure of the number

9 characters: the letter X, Y or Z, 7 digits and a control letter, written X1234567L. The initial X was used first, then Y, and Z from 2008 on.

The check digit, step by step

Replace the initial letter with a digit (X = 0, Y = 1, Z = 2), write it before the 7 digits to get an 8-digit number, divide by 23 and take the letter at the position of the remainder in the table TRWAGMYFPDXBNJZSQVHLCKE: the same as the DNI.

Worked example with X2301127T:

The initial X becomes 0, so the number is 2301127; 2301127 divided by 23 leaves 0; position 0 of the letter table is "T".

X=0  Y=1  Z=2

Valid examples

Five numbers generated by the tool with a fixed seed, each one accepted by its validator:

Without maskValidator
X2301127Tvalid
Y2229942Zvalid
Y6586234Zvalid
Z1625937Avalid
Y0834163Jvalid

How the validator answers

The same first example, one with a changed ending and one truncated, checked by the validator:

ValueResultReason
X2301127Tvalid-
X2301127Uinvalidthe check digit should be T
X230112invalidwrong format: X, Y or Z, 7 digits and a letter

Common mistakes

  • Forgetting to convert the initial letter is the usual bug: validating "X1234567" as if X were a digit, or dropping it, gives the wrong letter.
  • Initials other than X, Y and Z are not NIE: an old validator that only accepts X will reject all the recent Z numbers.

Frequently asked questions

What is the difference between NIE and NIF?

The NIF is the tax number: for Spaniards it is the DNI, for foreigners it is the NIE. The NIE is the identity number of foreigners; its control letter works like the DNI's.

Can a generated NIE be used to rent or open an account?

No. It has the correct format and letter but was never issued to anyone, so it is only useful to test forms and validations.

Can I use a Spanish NIE number generated here in a real system?

No. A number such as X2301127T has a correct format and check, but nobody was issued it, and it may coincide with a real holder by chance. Use it in test environments, demos and automated tests that never leave your control.

Report a problem