Created
December 20, 2012 19:31
-
-
Save mahizsas/4347953 to your computer and use it in GitHub Desktop.
Controllo Codice Fiscale
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
function controllaCF(cf) | |
{ | |
var validi, i, s, set1, set2, setpari, setdisp; | |
if( cf == '' ) return ''; | |
cf = cf.toUpperCase(); | |
if( cf.length != 16 ) | |
return "La lunghezza del codice fiscale non è\n" | |
+"corretta: il codice fiscale dovrebbe essere lungo\n" | |
+"esattamente 16 caratteri.\n"; | |
validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; | |
for( i = 0; i < 16; i++ ){ | |
if( validi.indexOf( cf.charAt(i) ) == -1 ) | |
return "Il codice fiscale contiene un carattere non valido `" + | |
cf.charAt(i) + | |
"'.\nI caratteri validi sono le lettere e le cifre.\n"; | |
} | |
set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX"; | |
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 "Il codice fiscale non è corretto:\n"+ | |
"il codice di controllo non corrisponde.\n"; | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment