Skip to content

Instantly share code, notes, and snippets.

@Rigo85
Forked from lgaticaq/dni_peru_validate.js
Created May 9, 2020 21:57
Show Gist options
  • Save Rigo85/8fc7094248220193279b7b0172b0d180 to your computer and use it in GitHub Desktop.
Save Rigo85/8fc7094248220193279b7b0172b0d180 to your computer and use it in GitHub Desktop.
Función para validar DNI Peruano
/**
* Verifica que un DNI de Perú sea valido
* @param {String} data DNI
* @returns {Boolean}
*/
const validate = data => {
const dni = data.replace('-', '').trim().toUpperCase()
if (!dni || dni.length < 9) return false
const multiples = [3, 2, 7, 6, 5, 4, 3, 2]
const dcontrols = {
numbers: [6, 7, 8, 9, 0, 1, 1, 2, 3, 4, 5],
letters: ['K', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
}
const numdni = dni.substring(0, dni.length - 1).split('')
const dcontrol = dni.substring(dni.length - 1)
const dsum = numdni.reduce((acc, digit, index) => {
acc += digit * multiples[index]
return acc
}, 0)
const key = 11 - (dsum % 11)
const index = (key === 11) ? 0 : key
if (/^\d+$/.test(dni)) {
return dcontrols.numbers[index] === parseInt(dcontrol, 10)
}
return dcontrols.letters[index] === dcontrol
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment