Skip to content

Instantly share code, notes, and snippets.

@TheHiddenHaku
Last active January 10, 2024 16:47
Show Gist options
  • Save TheHiddenHaku/6951634 to your computer and use it in GitHub Desktop.
Save TheHiddenHaku/6951634 to your computer and use it in GitHub Desktop.
Javascript function to check Italian Codice Fiscale
function codiceFISCALE(cfins)
{
var cf = cfins.toUpperCase();
var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
if (!cfReg.test(cf))
{
return false;
}
var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
var s = 0;
for( i = 1; i <= 13; i += 2 )
s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
for( i = 0; i <= 14; i += 2 )
s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
if ( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
return false;
return true;
}
@clixclix2
Copy link

Please note that there are special cases in which the character at position #15 could be a letter. So, this code is not correct.

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