/// <summary> /// Returns the PhoneNumbers.PhoneNumber object if it is a valid number /// </summary> /// <param name="phoneUtil">PhoneUtil instance</param> /// <param name="numberString">The number to validate against</param> /// <param name="regionCodes">The regions to check</param> /// <returns>Null if phonenumber was invalid</returns> public static PhoneNumbers.PhoneNumber GetValidNumber(this PhoneNumbers.PhoneNumberUtil phoneUtil, string numberString, string[] regionCodes) { foreach (var regionCode in regionCodes) { var phoneNumber = ParseFromString(phoneUtil, numberString, regionCode); if (phoneNumber == null) { continue; } if (phoneUtil.IsValidNumberForRegion(phoneNumber, regionCode)) { return(phoneNumber); } } return(null); }
/// <summary> /// Tries get a valid number for the regions passed in /// </summary> /// <param name="phoneUtil">PhoneUtil instance</param> /// <param name="numberString">Phonenumber</param> /// <param name="regionCodes">The region code for global networks</param> /// <param name="phoneNumber">PhoneNumbers.PhoneNumber object of the number passed in</param> /// <returns>True if successful; else false</returns> public static bool TryGetValidNumber(this PhoneNumbers.PhoneNumberUtil phoneUtil, string numberString, string[] regionCodes, out PhoneNumbers.PhoneNumber phoneNumber) { phoneNumber = null; foreach (var regionCode in regionCodes) { phoneNumber = ParseFromString(phoneUtil, numberString, regionCode); if (phoneNumber == null) { continue; } if (phoneUtil.IsValidNumberForRegion(phoneNumber, regionCode)) { return(true); } } return(false); }