How to validate spanish id using Javascript

validar dni español con código javascript id using javascript


Validate Spanish Id using Javascript Code

This tutorial is about how to validate a Spanish ID using Javascript code.  Each Spanish identity card – National Identity Document – has a number and a letter. So, the validation of the Spanish id using javascript code will consist of checking if the number will correspond with the letter. The Spanish Government uses a formula to assign Ids and this exercise is going to verify and validate if they are correct or not.

To validate the correct id of a Spanish citizen we can also choose other programming languages but this post is about how to validate an id using JavaScript language.

The code is based on a mathematical algorithm where the entire number of any given ID is divided by 23 and the remainder of such division will indicate the position in the array that will correspond to the right letter that a correct ID should have.

In the first part, we see a prompt that will collect the information inserted by the user, in this case, the number and the letter. That user input is stored in the variable numero.  We establish some requirements on the input, such as the prefix parse, which will convert into an integer – a number without decimals – the data inserted.


//we ask the user to insert his id number
var numero = parseInt(prompt("Please insert your dni number"));

//if the number inserted is greater than 99999999 we let the user know
alert("The id number inserted is not valid");
//the user inserts the letter of the id
var letra_dni = prompt("Insert the letter associated to that number please").toUpperCase();

The data inserted will go through a filter or condition, which is that the number should not  be greater than 99999999.

If the first part of the condition is met, then another prompt is shown to ask the user to insert the letter of the ID.

Formula to Verify Spanish Id using Javascript

The second part of the code is about a mathematical process where a calculation occurs. This calculation is based on obtaining the remainder of dividing the number inserted as ID by the number 23.

As mentioned above the remainder of such division will indicate the letter that should correspond to the number inserted. The letter is selected from an array of letters in the variable letras.

Finally, we explain that the algorithm establishes the correct letter according to the remainder of the division and the position of the letter in the array var letras. This array is set up by the Spanish Government.

For example, if the remainder of the division is 0, then the correct letter of the ID should be “T”; which is the position 0 of the “T” in the array. If the remainder of the division is 3, then the correct letter for the ID should be “A”, which is the element 3 in the array letras.


//we calculate the rest of the division to know the position within the array
var resto = numero %23;

var letras = [‘T’, ‘R’, ‘W’, ‘A’, ‘G’, ‘M’, ‘Y’, ‘F’, ‘P’, ‘D’, ‘X’, ‘B’, ‘N’, ‘J’, ‘Z’, ‘S’, ‘Q’, ‘V’, ‘H’, ‘L’, ‘C’, ‘K’, ‘E’, ‘T’]; 

letras = letras.join("");

var encontrado = letras.charAt(resto);

if(letra_dni == encontrado) {
alert("El dni insertado " + numero + "con letra " + letra_dni + " es CORRECTO");
alert("El dni insertado " + numero + "con letra " + letra_dni + " es INCORRECTO");

Lastly, the letter entered by the user is compared to the correct letter calculated by the algorithm. If they do not match, a message is displayed indicating that the number and letter inserted are not correct. Otherwise, a successful message would be displayed confirming that we have validated a Spanish id using javascript.

spanish id - dni español documento nacional de identidad

This post is an exercise that JavaScript students usually come across in their initial projects. If you wish to learn more about this language, here are some ebooks to learn JavaScript code.

Creatuwebpymes, is a web design company based in Lanzarote – Canary Islands. We ❤️ programming in JavaScript, hence the reason for this post.

creatuwebpymes quien somos

Francisco Brito Diaz

CEO of Creatuwebpymes, is a web design company and marketing digital based in Lanzarote – Canary Islands

Esta web utiliza cookies propias y de terceros para su correcto funcionamiento y para fines analíticos y para fines de afiliación y para mostrarte publicidad relacionada con sus preferencias en base a un perfil elaborado a partir de tus hábitos de navegación. Al hacer clic en el botón Aceptar, acepta el uso de estas tecnologías y el procesamiento de tus datos para estos propósitos.