Skip to content

Instantly share code, notes, and snippets.

@neiker
Last active April 16, 2024 22:08
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save neiker/874c197cd0cbb06efb328f3cbc6753b3 to your computer and use it in GitHub Desktop.
Save neiker/874c197cd0cbb06efb328f3cbc6753b3 to your computer and use it in GitHub Desktop.
Validación de CUIL (Argentina) en TypeScript / JavaScript
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;
}
@GonzaCoding
Copy link

Funciona perfecto, gracias!

@emanuel7benitez
Copy link

muchas gracias.. funciona de 10

@arshbot
Copy link

arshbot commented Jan 13, 2021

Does this work for CUIT as well?

@frandemona
Copy link

Does this work for CUIT as well?

Yes, they are the same digit. As NIF/NIE

@jpkontreras
Copy link

joya!
Saludos desde chile

@teebu
Copy link

teebu commented Mar 29, 2023

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;
}

@neiker
Copy link
Author

neiker commented Mar 29, 2023

@teebu elaborate "more correct"

@teebu
Copy link

teebu commented Mar 29, 2023

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