public Parse ( String numberToParse, String defaultRegion ) : |
||
numberToParse | String | |
defaultRegion | String | |
Résultat |
private static PhoneNumbers.PhoneNumber ParseFromString(PhoneNumbers.PhoneNumberUtil phoneUtil, string numberString, string regionCode) { try { return(phoneUtil.Parse(numberString, regionCode)); } catch (NumberParseException) { return(null); } }
/// <summary> /// Try gets the phonenumber formatted in E164 if it was valid for the first country as specified in order of countries passed in /// </summary> /// <param name="phoneUtil">PhoneNumberUtil instance</param> /// <param name="numberString">The phonenumber to get </param> /// <param name="countryCodes">The countries to check for a valid phonenumber</param> /// <param name="formattedPhoneNumber">The phonenumber formatted in E164</param> /// <returns>True if successfully retrieves the formatted phonenumber; else false</returns> public static bool TryGetFormattedPhoneNumber(this PhoneNumbers.PhoneNumberUtil phoneUtil, string numberString, string[] countryCodes, out string formattedPhoneNumber) { formattedPhoneNumber = null; foreach (var countryCode in countryCodes) { var phoneNumber = phoneUtil.Parse(numberString, countryCode); if (phoneUtil.IsValidNumber(phoneNumber)) { formattedPhoneNumber = phoneUtil.Format(phoneNumber, PhoneNumberFormat.E164); return(true); } } return(false); }
/// <summary> /// Tests whether a phone number matches a valid pattern. Note this doesn't verify the number /// is actually in use, which is impossible to tell by just looking at a number itself. /// </summary> /// <param name="phoneNumber">The phone number in national or international format, that we want to validate.</param> /// <param name="defaultRegion">The region to use, if the phoneNumber is in national format.</param> /// <returns>A bool that indicates whether the number is of a valid pattern.</returns> public bool IsValid(string phoneNumber, string defaultRegion) { if (string.IsNullOrWhiteSpace(phoneNumber)) { return(false); } lock (Locker) { try { var number = _fnu.Parse(phoneNumber, defaultRegion); return(_fnu.IsValidNumber(number)); } catch (Exception e) { _logger.LogDebug(e, "{@phoneNumber}", phoneNumber); return(false); } } }