Skip to content

Instantly share code, notes, and snippets.

@jrmoran
Created October 28, 2011 00:48
Show Gist options
  • Save jrmoran/1321329 to your computer and use it in GitHub Desktop.
Save jrmoran/1321329 to your computer and use it in GitHub Desktop.
Verificacion DUI
/*
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
@MarioAmaya
Copy link

Alguien que tenga el codigo validador del NRC de El Salvador

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment