/// <summary> /// Check whether phone number is valid or not for defined culture /// Culture denotes country language code i.e; NP /// </summary> /// <param name="number">number (string) to be validated as valid phone number</param> /// <param name="code"> /// Country code must be provided to verify the phone number based on country. /// It has two alphabetes which is universal. Code cannot be number, empty string or null. /// Invalid code will result into invalid phone number. /// Example of country code; Nepal = NP, United States = US, Denmark = DK /// </param> /// <returns>validation result with properties, ErrorMessage (string), Exception and Result (bool)</returns> public static ValidationResult <bool> IsPhoneNumber(this string number, string code) { var operation = number.ValidatePhoneNumber(); if (operation.Result && !string.IsNullOrEmpty(code) && code.Length.Equals(2) && Enum.TryParse(code, out CountryCodes countryCode)) { var integerValue = CheckPhoneNumber.GetNumbers(number); operation = ValidateWithCountry(integerValue, code) ? new ValidationResult <bool>(true, null, "Success") : new ValidationResult <bool>(false, null, $"Invalid phone number {number}."); } else { operation = new ValidationResult <bool>(false, null, $"Invalid country code {code}."); } return(operation); }
/// <summary> /// /// </summary> /// <param name="number"></param> /// <returns></returns> private static ValidationResult <bool> ValidateWithDictionary(this string number) { bool result = false; foreach (var countryCode in Enum.GetNames(typeof(CountryCodes))) { var integerValue = CheckPhoneNumber.GetNumbers(number); result = ValidateWithCountry(integerValue, countryCode); if (result) { break; } } return(result ? new ValidationResult <bool>(true, null, "Success") : new ValidationResult <bool>(false, null, $"Invalid phone number {number}."));; }