Пример #1
0
        public static bool IsValid(string nationalId)
        {
            if (string.IsNullOrWhiteSpace(nationalId))
            {
                throw new ArgumentException($"'{nameof(nationalId)}' cannot be null or whitespace", nameof(nationalId));
            }
            nationalId = Digits.PersianToEnglishDigits(nationalId);
            if (!NATIONAL_ID_PATTERN.IsMatch(nationalId))
            {
                throw new FormatException(nameof(nationalId));
            }
            var r = nationalId
                    .Take(9)
                    .Select((digit, index) => (digit - '0') * (10 - index))
                    .Sum() % 11;

            return(nationalId.Last() == LAST_DIGIT[r]);
        }
Пример #2
0
        private static string CheckValidationAndExtract(string pan)
        {
            if (string.IsNullOrWhiteSpace(pan))
            {
                throw new ArgumentException($"'{nameof(pan)}' cannot be null or whitespace", nameof(pan));
            }
            string englishPan = Digits.PersianToEnglishDigits(pan);

            englishPan = englishPan.Replace(" ", "")
                         .Replace("-", "")
                         .Replace("_", "")
                         .Replace(",", "");

            if (englishPan.Count() != 16)
            {
                throw new ArgumentOutOfRangeException($"'{nameof(pan)}' should contains 16 digits.", nameof(pan));
            }
            if (!englishPan.All(x => char.IsDigit(x)))
            {
                throw new ArgumentException($"'{nameof(pan)}' contains invalid characters!", nameof(pan));
            }

            return(englishPan);
        }