Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save biagnei240698/8f6c628365b1a329651d7d228b18b2e2 to your computer and use it in GitHub Desktop.
Save biagnei240698/8f6c628365b1a329651d7d228b18b2e2 to your computer and use it in GitHub Desktop.
A simple function to validate guatemalan NIT using both, regex and math.
function valNit(nit){
var nd, add=0;
if(nd = /^(\d+)\-?([\dk])$/i.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(valNit('3602978-5'));
console.log(valNit('576937-K'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment