Fiscal Code Validator can be used to perform a syntactic validation of Italian fiscal codes.
The FiscalCode
class can be initialized by passing the complete fiscal code in the constructor.
The code is decomposed into the constituent units (first name, last name, gender, birth date, and birth place).
Each piece of information is individually extracted from the string and the class properties are populated.
If the code provided is incorrect, some of the properties may be left null
.
FiscalCode fiscalCode = new FiscalCode("RSSLRA90D70L378H");
string lastName = fiscalCode.LastName; // RSS
string firstName = fiscalCode.FirstName; // LRA
DateTime? date = fiscalCode.Date; // 1990-04-30
Gender? gender = fiscalCode.Gender; // Gender.Female
Place place = fiscalCode.Place; // Trento
The FiscalCode
class provides some methods to check if the fiscal code data partially corresponds to the owner’s personal information.
It should be noted that these functions do not provide information on the overall validity of the code.
bool result = fiscalCode.matchLastName("Rossi"); // true
bool result = fiscalCode.matchFirstName("Laura"); // true
bool result = fiscalCode.matchGender(Gender.Male); // false
bool result = fiscalCode.matchDate(new DateTime(1990, 5, 30)); // false
bool result = fiscalCode.matchPlace(Utility.GetPlace("L378")); // true
The isValid()
method performs a syntactic validation of each section of the code, individually.
If no parameters are provided, the method checks if all the properties of the FiscalCode
instance have been populated correctly
and then verifies the validity of the check digit.
bool result = fiscalCode.isValid(); // true
If one or more parameters are provided, the method performs the former validation and then checks whether the data provided matches the code information.
bool result = fiscalCode.isValid(firstName: "Laura", gender: Gender.Female); // true
bool result = fiscalCode.isValid(gender: Gender.Male); // false