/// <summary> /// Attempts to normalized the given phone number for the given region code. /// </summary> /// <param name="phoneNumber">The phone number to normalize.</param> /// <param name="regionCode"> /// Region that we are expecting the number to be from. /// This is only used if the number being parsed is not written in international format. /// </param> /// <param name="normalized">The normalized string or the original string if normalization failed.</param> /// <returns>True if normalization succeeds, otherwise false.</returns> public static bool TryNormalizeRc(string phoneNumber, string regionCode, out string normalized) { EnsurePhoneUtil(); EnsureRegionTimezoneMap(); normalized = phoneNumber; if (String.IsNullOrWhiteSpace(phoneNumber)) { return(false); } phoneNumber = phoneNumber .Trim() .Replace("o", "0") .Replace("O", "0"); try { var parsedNumber = _phoneUtil.Parse(phoneNumber, regionCode); if (_phoneUtil.IsValidNumber(parsedNumber)) { normalized = $"+{parsedNumber.CountryCode}{parsedNumber.NationalNumber}"; return(true); } // Check the case where country code is included without the + or 00 prefix // as libphone wont recoginize as country code unless we add one of the above // prefixes. else if (phoneNumber.Length >= 10 && Regex.IsMatch(phoneNumber, @"^\d")) { foreach (var countryCode in _phoneUtil.GetSupportedCallingCodes()) { var region = _phoneUtil.GetRegionCodeForCountryCode(countryCode); var exampleNumber = _phoneUtil.GetExampleNumberForType(region, PhoneNumberType.MOBILE).NationalNumber; var sCountryCode = countryCode.ToString(); var totalLength = sCountryCode.Length + exampleNumber.ToString().Length; if (phoneNumber.StartsWith(sCountryCode) && phoneNumber.Length == totalLength) { return(TryNormalizeRc($"+{phoneNumber}", regionCode, out normalized)); } } } else { return(false); } } catch { return(false); } return(false); }