diff --git a/csharp/PhoneNumbers.Test/TestExampleNumbers.cs b/csharp/PhoneNumbers.Test/TestExampleNumbers.cs index 7862881..9ce1149 100644 --- a/csharp/PhoneNumbers.Test/TestExampleNumbers.cs +++ b/csharp/PhoneNumbers.Test/TestExampleNumbers.cs @@ -43,6 +43,24 @@ protected void SetUp() wrongTypeCases.Clear(); } + [Test] + public void TestParseMultipleRegions() + { + var usPhoneNumber = "14109992222"; + var canadaPhoneNumber = "14039782234"; + String matchedUsRegionCode; + String matchedCanadianRegionCode; + + // Tests + var usValid = phoneNumberUtil.IsValidNumber(usPhoneNumber, new List() { "US", "CA" }, out matchedUsRegionCode); + var canadaValid = phoneNumberUtil.IsValidNumber(canadaPhoneNumber, new List() { "US", "CA" }, out matchedCanadianRegionCode); + + Assert.IsTrue(usValid); + Assert.IsTrue(canadaValid); + Assert.AreEqual(matchedUsRegionCode, "US"); + Assert.AreEqual(matchedCanadianRegionCode, "CA"); + } + /** * @param exampleNumberRequestedType type we are requesting an example number for * @param possibleExpectedTypes acceptable types that this number should match, such as diff --git a/csharp/PhoneNumbers/PhoneNumberUtil.cs b/csharp/PhoneNumbers/PhoneNumberUtil.cs index ca81e69..c92663b 100644 --- a/csharp/PhoneNumbers/PhoneNumberUtil.cs +++ b/csharp/PhoneNumbers/PhoneNumberUtil.cs @@ -1942,6 +1942,56 @@ private bool IsNumberMatchingDesc(String nationalNumber, PhoneNumberDesc numberD return possibleNumberPatternMatch.Success && nationalNumberPatternMatch.Success; } + /// + /// Checks to see if the given phone number is valid for the given regionCode + /// + /// + /// + /// True if valid, false if not (or not able to parse phone number based on region) + public bool IsValidNumber(String phoneNumber, String regionCode) + { + if (String.IsNullOrEmpty(regionCode)) + return false; + + var pn = Parse(phoneNumber, regionCode); + if (pn != null) + return IsValidNumberForRegion(pn, regionCode); + + return false; + } + + /// + /// Checks to see if the given phone number is valid for one of the regionCodes. If valid, matchedRegionCode will be populated + /// with the matched region code for the valid phone number. + /// + /// + /// + /// If a valid number, this will be populated with the matching region code. + /// True if valid, false if not (or not able to parse phone number based on one of the regions) + public bool IsValidNumber(String phoneNumber, IList regionCodes, out String matchedRegionCode) + { + matchedRegionCode = String.Empty; + + if (regionCodes == null || regionCodes.Count == 0) + return false; + + var phoneNumbers = new List(); + foreach (var regionCode in regionCodes) + { + var pn = Parse(phoneNumber, regionCode); + if (pn != null) + { + if (IsValidNumberForRegion(pn, regionCode)) + { + matchedRegionCode = regionCode; + phoneNumbers.Add(pn); + } + } + } + + return phoneNumbers.Count > 0; + } + /** * 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.