|
There is a hash algorithm which makes us able to validate T.C. Identity Number that we take as an input in our projects. You can find the method that checks this hash below.
private bool ValidateNumber(string parNumber)
{
for (int i = 0; i < parNumber.Length; i++)
{
if (!(char.IsNumber(parNumber[i]))) return false;
}
return true;
}
public void ValidateTCIdentityNumber(string TCIdentityNumber)
{
if (ValidateNumber(TCIdentityNumber.Trim()))
{
if (TCIdentityNumber.Trim().Length < 11)
{
throw new Exception("T.C. Kimlik Numarası 11 karakterden az olamaz. Lütfen kontrol ediniz.");
}
else
{
decimal identityNumberDecimal = Convert.ToDecimal(TCIdentityNumber.Trim());
decimal temp = Math.Floor(identityNumberDecimal / 100);
decimal temp1 = Math.Floor(identityNumberDecimal / 100);
int[] D = new int[10];
for (int n = 9; n >= 1; n--)
{
D[n] = (int)temp1 % 10;
temp1 = Math.Floor(temp1 / 10);
}
int oddSum = D[9] + D[7] + D[5] + D[3] + D[1];
int evenSum = D[8] + D[6] + D[4] + D[2];
int total = oddSum * 3 + evenSum;
int ChkDigit1 = (10 - (total % 10)) % 10;
oddSum = ChkDigit1 + D[8] + D[6] + D[4] + D[2];
evenSum = D[9] + D[7] + D[5] + D[3] + D[1];
total = oddSum * 3 + evenSum;
int ChkDigit2 = (10 - (total % 10)) % 10;
temp = (temp * 100) + (ChkDigit1 * 10) + ChkDigit2;
if (temp != identityNumberDecimal)
{
throw new Exception("T.C. Kimlik Numaranızı kontrol ediniz.");
}
}
}
else
{
throw new Exception("T.C. Kimlik Numarası yalnızca rakamlardan oluşmalıdır. Lütfen kontrol ediniz.");
}
}
|