Created
October 28, 2011 00:48
-
-
Save jrmoran/1321329 to your computer and use it in GitHub Desktop.
Verificacion DUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
DUI = 00016297-5 | |
Posiciones -> 9 8 7 6 5 4 3 2 | |
DUI -> 0 0 0 1 6 2 9 7 | |
DV = 5 | |
sum: (9*0) + (8*0) + (7*0) + (6*1) + (5*6) + (4*2) + (3*9) + (2*7) = 85 | |
residuo: (85 % 10) = 5 | |
resta: 10 - residuo = 5 | |
IF DV == Resta THEN true ELSE false | |
*/ | |
var isDUI = function(str){ | |
var regex = /(^\d{8})-(\d$)/, | |
parts = str.match(regex); | |
// verficar formato y extraer digitos junto al digito verificador | |
if(parts !== null){ | |
var digits = parts[1], | |
dig_ve = parseInt(parts[2], 10), | |
sum = 0; | |
// sumar producto de posiciones y digitos | |
for(var i = 0, l = digits.length; i < l; i++){ | |
var d = parseInt(digits[i], 10); | |
sum += ( 9 - i ) * d; | |
} | |
return dig_ve === 10 - ( sum % 10 ); | |
}else{ | |
return false; | |
} | |
}; | |
isDUI('00016297-5'); // true | |
isDUI('12345678-1'); // false | |
isDUI('123456789-1'); // false | |
isDUI('12345678-12'); // false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alguien que tenga el codigo validador del NRC de El Salvador