Last active
April 16, 2024 22:08
-
-
Save neiker/874c197cd0cbb06efb328f3cbc6753b3 to your computer and use it in GitHub Desktop.
Validación de CUIL (Argentina) en TypeScript / JavaScript
This file contains hidden or 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
export function cuilValidator(cuil: string): boolean { | |
if (cuil.length !== 11) { | |
return false; | |
} | |
const [checkDigit, ...rest] = cuil | |
.split('') | |
.map(Number) | |
.reverse(); | |
const total = rest.reduce( | |
(acc, cur, index) => acc + cur * (2 + (index % 6)), | |
0, | |
); | |
const mod11 = 11 - (total % 11); | |
if (mod11 === 11) { | |
return checkDigit === 0; | |
} | |
if (mod11 === 10) { | |
return false; | |
} | |
return checkDigit === mod11; | |
} |
muchas gracias.. funciona de 10
Does this work for CUIT as well?
Does this work for CUIT as well?
Yes, they are the same digit. As NIF/NIE
joya!
Saludos desde chile
is this correct?
This function seems to be more correct:
function validateCUITorCUIL(cuitOrCuil) {
if (!/^\d{2}-\d{8}-\d$/.test(cuitOrCuil)) {
return false;
}
var cuitOrCuilNoDash = cuitOrCuil.replace(/-/g, '');
var checkDigit = parseInt(cuitOrCuilNoDash.charAt(10));
var checkSum = 0;
var multiplier = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2];
for (var i = 0; i < 10; i++) {
checkSum += parseInt(cuitOrCuilNoDash.charAt(i)) * multiplier[i];
}
var result = (11 - checkSum % 11) % 11;
return result === checkDigit;
}
@teebu elaborate "more correct"
I was looking for a validator, and stumbled on to this library https://github.com/brielov/cuit/tree/master/src and checked their tests.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Funciona perfecto, gracias!