Skip to content

Instantly share code, notes, and snippets.

@eddwinpaz
Forked from lgaticaq/dni_peru_validate.js
Created April 16, 2020 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddwinpaz/daf806c7638a762d0b4488a17f3a4524 to your computer and use it in GitHub Desktop.
Save eddwinpaz/daf806c7638a762d0b4488a17f3a4524 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