Mega Code Archive

 
Categories / Delphi / Examples
 

How to check if an Italian Fiscal Code is valid

Title: How to check if an Italian Fiscal Code is valid. Question: A simple function that asks a Fiscal Code (16 digits) and calculates if it's valid. Returns a boolean value. Answer: Just use this and specify in the parameter the Fiscal Code you want to test. Function VerifyCodFisc( Code : String ) : Boolean; Var tmpInt : Integer; IdX : Integer; AppoNum : Integer; CheckDigit : String; Begin tmpInt := 0; IdX := 1; Code := UpperCase( Code ); Repeat AppoNum := Pos( Copy( Code, IdX, 1 ), 'B1A0KKPPLLC2QQD3RRE4VVOOSSF5TTG6UUH7MMI8NNJ9WWZZYYXX' ); tmpInt := tmpInt + ( ( AppoNum - 1 ) And ($7FFE) ) Div 2; Inc( IdX ); If ( IdX 15 ) Then Break; AppoNum := Pos( Copy( Code, IdX, 1 ), 'A0B1C2D3E4F5G6H7I8J9KKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ' ); tmpInt := tmpInt + ( ( AppoNum - 1 ) And ($7FFE) ) Div 2; Inc( IdX ) Until ( Code = '' ); // Infinite loop, exits with the Break instruction. tmpInt := tmpInt Mod 26; CheckDigit := Copy( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', ( tmpInt + 1 ), 1 ); If ( CheckDigit = Code[ Length( Code ) ] ) Then Result := True Else Result := False; End;