public CprError CheckDate(string cprTxt) { CprError cprError = CprError.NoError; string date = cprTxt.Substring(0, 6); string dateyfied = default; for (int i = 0; i < date.Length; i++) { if (i == 2) { dateyfied = date.Insert(2, "-"); } if (i == 4) { dateyfied = dateyfied.Insert(5, "-"); } } bool validDate = DateTime.TryParse(dateyfied, out DateTime res); if (!validDate) { cprError = CprError.DateError; //throw new Exception("Date is not valid: " + dateyfied); } return(cprError); }
private CprError Check11Test(string cprTxt) { /// <summary> /// The CPR check sum algorithm is calculated by mulitiplying each digit with a factor /// and then add all results and divide the sum by 11. /// Factors: 4327654321 /// CPR: 0609240121 /// Sum: 0 + 18 + 0 + 72 + 12 + 20 + 0 + 1 + 4 + 1 = 121 / 11 = 11.0 -> CPR is OK /// </summary> /// CprError cprError = CprError.NoError; try { int sum = 0; for (int i = 0; i < 3; i++) { sum += int.Parse(cprTxt.Substring(i, 1)) * (4 - i); } for (int i = 3; i < 10; i++) { sum += int.Parse(cprTxt.Substring(i, 1)) * (10 - i); } if (sum % 11 != 0) { cprError = CprError.Check11Error; } } catch (Exception) { cprError = CprError.FormatError; } return(cprError); }
/// <summary> /// This method validates a danish cpr number. /// Checks performed: /// Must contain 10 digits. /// First 6 digits must be a valid date. /// The 11-rule (Checksum test) /// </summary> /// <param name="cprTxt">a string containing a cprnumber.</param> /// <param name="error">Indicates type of error if any.</param> /// <returns>Returns true if no format errors found</returns> public bool CheckCPR(string cprTxt, out CprError error) { CprError cprError = CprError.NoError; if (allow9) { if (cprTxt == "9999999999") { error = cprError; return(true); } } cprError = FormatTest(cprTxt); if (cprError == CprError.NoError) { cprError = DateTest(cprTxt); } if (cprError == CprError.NoError) { cprError = Check11Test(cprTxt); } error = cprError; return(cprError == CprError.NoError); }
/// <summary> /// This method validates a danish cpr number. /// Checks performed: /// Must contain 10 digits. /// First 6 digits must be a valid date. /// The 11-rule (Checksum test) /// </summary> /// <param name="cprTxt">a string containing a cprnumber.</param> /// <param name="error">Indicates type of error if any.</param> /// <returns>Returns true if no format errors found</returns> public static bool CheckCPR(string cprTxt, out CprError error) { CprError cprError = FormatTest(cprTxt); if (cprError == CprError.NoError) cprError = DateTest(cprTxt); if (cprError == CprError.NoError) cprError = Check11Test(cprTxt); error = cprError; return (cprError == CprError.NoError); }
public CprError CheckFormat(string cprTxt) { CprError cprError = CprError.NoError; if (cprTxt.Length != 10) { cprError = CprError.FormatError; //throw new FormatException(); } return(cprError); }
private CprError CheckFormat10Test(string cprTxt) { CprError cprError = CprError.NoError; if (cprTxt.Length != 10) { cprError = CprError.FormatError; } return(cprError); }
private CprError CheckDateTest(string cprTxt) { CprError cprError = CprError.NoError; try { DateTime cprDate = new DateTime(Int32.Parse(cprTxt.Remove(0, 4).Remove(2)), Int32.Parse(cprTxt.Remove(0, 2).Remove(2)), Int32.Parse(cprTxt.Remove(2))); } catch (ArgumentOutOfRangeException) { cprError = CprError.DateError; } return(cprError); }
public bool Check(string cprTxt, out CprError error) { CprError cprError = FormatTest(cprTxt); if (cprError == CprError.NoError) { cprError = DateTest(cprTxt); } if (cprError == CprError.NoError) { cprError = Check11Test(cprTxt); } error = cprError; return(cprError == CprError.NoError); }
public bool Check(string cprTxt, out CprError error) { DateTime formatedDate; if (CheckFormat(cprTxt) == CprError.FormatError) { error = CprError.FormatError; return false; } if(CheckDate(cprTxt) == CprError.DateError) { error = CprError.DateError; return false; } error = Check11Test(cprTxt); return true; }
public bool Check(string cprTxt, out CprError error) { error = Check11Test(cprTxt); if (error != CprError.NoError) { return(false); } error = CheckDateTime(cprTxt); if (error != CprError.NoError) { return(false); } error = CheckFormat(cprTxt); if (error != CprError.NoError) { return(false); } return(true); }
/// <summary> /// The CPR check sum algorithm is calculated by mulitiplying each digit with a factor /// and then add all results and divide the sum by 11. /// Factors: 4327654321 /// CPR: 0609240121 /// Sum: 0 + 18 + 0 + 72 + 12 + 20 + 0 + 1 + 4 + 1 = 121 / 11 = 11.0 -> CPR is OK /// </summary> /// private CprError Check11Test(string cprTxt) { CprError cprError = CprError.NoError; int sum = 0; for (int i = 0; i < 3; i++) { sum += int.Parse(cprTxt.Substring(i, 1)) * (4 - i); } for (int i = 3; i < 10; i++) { sum += int.Parse(cprTxt.Substring(i, 1)) * (10 - i); } if (sum % 11 != 0) { cprError = CprError.Check11Error; } return(cprError); }
public bool Check(string cprTxt, out CprError error) { error = CprError.NoError; if (CheckFormat10Test(cprTxt) != CprError.NoError) { error = CprError.FormatError; } if (CheckDateTest(cprTxt) != CprError.NoError) { error = CprError.DateError; } if (Check11Test(cprTxt) != CprError.NoError) { error = CprError.Check11Error; } return(error == CprError.NoError); }
private CprError FormatTest(string cprTxt) { Regex regExp; Match match; string cprFormat = "[0-9]{10}"; CprError cprError = CprError.NoError; if (cprTxt.Length != 10) { cprError = CprError.FormatError; } else { regExp = new Regex(cprFormat); match = regExp.Match(cprTxt); if (!match.Success) { cprError = CprError.FormatError; } } return(cprError); }
public bool Check(string cprTxt, out CprError error) { bool validCPR = true; error = CheckFormat(cprTxt); if (error != CprError.NoError) { validCPR = false; } error = CheckDate(cprTxt); if (error != CprError.NoError) { validCPR = false; } error = Check11Test(cprTxt); if (error != CprError.NoError) { validCPR = false; } return(validCPR); }
private CprError DateTest(string cprTxt) { string dayStr = cprTxt.Substring(0, 2); string monthStr = cprTxt.Substring(2, 2); string yearStr = cprTxt.Substring(4, 2); CprError cprError = CprError.NoError; int day; int month; int year; try { day = int.Parse(dayStr); month = int.Parse(monthStr); year = int.Parse(yearStr); DateTime dt = new DateTime(year, month, day); } catch (Exception) { cprError = CprError.DateError; } return(cprError); }
private static CprError FormatTest(string cprTxt) { Regex regExp; Match match; string cprFormat = "[0-9]{10}"; CprError cprError = CprError.NoError; // Test length if (cprTxt.Length != 10) { cprError = CprError.FormatError; } else { // Make sure we have 10 digits regExp = new Regex(cprFormat); match = regExp.Match(cprTxt); if (!match.Success) { cprError = CprError.FormatError; } } return(cprError); }