Exemplo n.º 1
0
        private void Initialize(string identityNumber)
        {
            if (identityNumber.IsNullOrEmptyTrimmed())
            {
                return;
            }

            const string numericPattern = @"^\d{13}$";

            IdentityNumber = identityNumber.Trim();
            if (IdentityNumber.Length != 13)
            {
                return;
            }
            var isMatch = Regex.IsMatch(IdentityNumber, numericPattern);

            if (!isMatch)
            {
                return;
            }

            var month = int.Parse(identityNumber.Substring(2, 2));

            if (month < 1 || month > 12)
            {
                return;
            }

            var day = int.Parse(identityNumber.Substring(4, 2));

            if (day < 1 || day > 31)
            {
                return;
            }

            var digits = new int[13];

            for (var i = 0; i < 13; i++)
            {
                digits[i] = int.Parse(IdentityNumber.Substring(i, 1));
            }
            var control1 = digits.Where((v, i) => i % 2 == 0 && i < 12).Sum();
            var second   = string.Empty;

            digits.Where((v, i) => i % 2 != 0 && i < 12).ToList().ForEach(v => second += v.ToString(CultureInfo.InvariantCulture));
            var string2  = (int.Parse(second) * 2).ToString(CultureInfo.InvariantCulture);
            var control2 = string2.Select((t, i) => int.Parse(string2.Substring(i, 1))).Sum();
            var control  = (10 - (control1 + control2) % 10) % 10;

            if (digits[12] != control)
            {
                return;
            }

            IsFemale       = digits[6] < 5;
            IsMale         = !IsFemale;
            IsSouthAfrican = digits[10] == 0;
            IsValid        = true;
        }
Exemplo n.º 2
0
        private void Initialize(string identityNumber)
        {
            IdentityNumber = (identityNumber ?? string.Empty).Replace(" ", "");
            if (IdentityNumber.Length == 13)
            {
                int[] digits = new int[13];
                for (int i = 0; i < 13; i++)
                {
                    digits[i] = int.Parse(IdentityNumber.Substring(i, 1));
                }
                int    control1 = digits.Where((v, i) => i % 2 == 0 && i < 12).Sum();
                string second   = string.Empty;
                digits.Where((v, i) => i % 2 != 0 && i < 12).ToList().ForEach(v =>
                                                                              second += v.ToString());
                string string2  = (int.Parse(second) * 2).ToString();
                int    control2 = 0;
                for (int i = 0; i < string2.Length; i++)
                {
                    control2 += int.Parse(string2.Substring(i, 1));
                }
                int control = (10 - ((control1 + control2) % 10)) % 10;
                if (digits[12] == control)
                {
                    BirthDate = DateTime.ParseExact(IdentityNumber
                                                    .Substring(0, 6), "yyMMdd", null);
                    Gender         = digits[6] < 5 ? "Female" : "Male";
                    IsSouthAfrican = digits[10] == 0;
                    if (BirthDate > DateTime.Now)
                    {
                        IsValid = false;
                        return;
                    }

                    Age             = CalculateAge(BirthDate);
                    AgeToLongString = CalculateAgeToLongString(BirthDate);
                    IsValid         = true;
                }
            }
            validateRSAidnumber(identityNumber);
        }