Last active
January 10, 2024 16:47
-
-
Save TheHiddenHaku/6951634 to your computer and use it in GitHub Desktop.
Javascript function to check Italian Codice Fiscale
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please note that there are special cases in which the character at position #15 could be a letter. So, this code is not correct.