How to validate spanish id using Javascript tutorial | 2022

validar dni javascript en blog diseño páginas web - validate spainsh id using javascript
k

10 May 2020

Comparte | Share

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.

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 – number without decimals – the data inserted.

JS


//se le pide al usuario que introduzca su dni
var numero = parseInt(prompt("Introduzca su dni por favor"));

if(numero>"99999999"){
//si el numero es mayor pues se le comunica al usuario
alert("El dni introducido no es valido");
}
else{
//el usuario inserta la letra del dni
var letra_dni = prompt("Introduzca la letra de su dni").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 os such division will indicate us 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 correct letter is established by the algorithm 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.

JS


//calculamos la diferencia de la división para saber la posición
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");
}
else{
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.

We are a web design company based in Lanzarote, Canary Islands and we love web programming. Hence we share this tutorial for validation of Spanish id using Javascript code.

Comparte | Share
creatuwebpymes quien somos

Francisco Brito Diaz

CEO of creatuwebpymes.com, web design company & digital marketing in Canary Islands.