Last active
August 15, 2023 13:43
-
-
Save rodrigopolo/0807e96a5331eb17e819f485d9a3c891 to your computer and use it in GitHub Desktop.
A simple function to validate guatemalan NIT using both, regex and math.
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
/* | |
* These functions validate whether a string corresponds to a Guatemalan tax | |
* identification number, also known as 'Numero de Identificación | |
* Tributaria' (NIT). They employ regular expressions for this purpose, utilizing | |
* the metacharacter shorthand '\d' instead of '[0-9]' to save space. | |
* The approach is highly optimized for bandwidth reduction; readability and | |
* maintenance are not prioritized over optimization. You can observe how this | |
* regular expression works through the following links: | |
* - https://regex101.com/r/d5YFJE/1 | |
* - https://regexr.com/7ieic | |
*/ | |
// New JS ES6 function with improved regex | |
function valNit(nit){ | |
let nd, add=0; | |
if(nd = /^(\d+)\-?([\dkK])$/.exec(nit)){ | |
nd[2] = (nd[2].toLowerCase()==='k')?10:parseInt(nd[2]); | |
for (let i = 0; i < nd[1].length; i++) { | |
add += ( (((i-nd[1].length)*-1)+1) * nd[1][i] ); | |
} | |
return ((11 - (add % 11)) % 11) === nd[2]; | |
}else{ | |
return false; | |
} | |
} | |
// Old JS ES5 function with improved regex | |
function valNit(nit){ | |
var nd, add=0; | |
if(nd = /^(\d+)\-?([\dkK])$/.exec(nit)){ | |
nd[2] = (nd[2].toLowerCase()=='k')?10:parseInt(nd[2]); | |
for (var i = 0; i < nd[1].length; i++) { | |
add += ( (((i-nd[1].length)*-1)+1) * nd[1][i] ); | |
} | |
return ((11 - (add % 11)) % 11) == nd[2]; | |
}else{ | |
return false; | |
} | |
} | |
console.log(validateTaxID('3602978-5')); // Outputs: true | |
console.log(validateTaxID('3602978-6')); // Outputs: false | |
console.log(validateTaxID('576937-K')); // Outputs: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A las órdenes @jorge-gomez7, tenía ratos de no ver la función, la actualicé un poco y arreglé un poco la expresión regular, y aprovecho para compartir la explicación de cómo se valida un NIT.
¿Cómo se Valida un NIT?
El NIT se compone de varios dígitos que puede estén seguidos de un guión
-
y un último número o letraK
que simboliza 10, este último caracter recibe el nombre de dígito verificador, por ejemplo:3602978-5
o36029785
sin el guión. A continuación, el proceso de verificación.3602978
16 + 21 + 36 + 10 + 0 +42 + 24 = 149
11 – 6 = 5
Tomando en cuenta que si el dígito verificador es
K
, este tiene un valor de10
, y que de lo contrario es sólo un dígito, si el resultado obtenido al final de la operación es igual al dígito verificador, significa que el NIT es válido, como en este caso en el que validamos el NIT3602978-5
que tiene como dígito verificador el número5
obtenido al final de las operaciones.Fuente: http://ficoaex.blogspot.com/2012/10/como-se-valida-un-nit.html