private static readonly int[] WeightsNoK1 = new[] { 3, 7, 6, 1, 8, 9, 4, 5, 2 };
private static readonly int[] WeightsNoK2 = new[] { 5, 4, 3, 2, 7, 6, 5, 4, 3, 2 };
public bool IsCivicRegistrationNumberValid(string civicNumber)
{
if (civicNumber == null)
return false;
var cleanedCivicNumber = civicNumber.Replace("-", "");
if (!IsNumeric(cleanedCivicNumber))
return false;
if (cleanedCivicNumber.Length != 11)
return false;
if (!IsDateValid(cleanedCivicNumber.Substring(0, 6), "ddMMyy", "nb-NO"))
return false;
int sum = 0;
for (int i = 0; i < 9; i++)
{
sum += WeightsNoK1[i] * int.Parse(cleanedCivicNumber.Substring(i, 1));
}
int k1 = 11 - sum % 11;
if (k1 == 11)
{
k1 = 0;
}
if (k1.ToString() != cleanedCivicNumber.Substring(9, 1))
{
return false;
}
sum = 0;
for (int i = 0; i < 10; i++)
{
sum += WeightsNoK2[i] * int.Parse(cleanedCivicNumber.Substring(i, 1));
}
int k2 = 11 - sum % 11;
if (k2 == 11)
{
k2 = 0;
}
if (k2.ToString() != cleanedCivicNumber.Substring(10, 1))
{
return false;
}
return true;
}
}
Fork
0 Feedback
You must log in before you can give any feedback
You must log in before you can post a comment


81
0




Mark 'c#' tag as 'like'
Mark 'c#' tag as 'ignore'