IsValidNumber() public method

public IsValidNumber ( PhoneNumber number ) : bool
number PhoneNumber
return bool
Ejemplo n.º 1
0
        /// <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);
                }
            }
        }
Ejemplo n.º 2
0
        /// <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);
        }
 public bool Verify(Leniency leniency, PhoneNumber number, String candidate, PhoneNumberUtil util)
 {
     switch (leniency)
     {
         case Leniency.POSSIBLE:
             return IsPossibleNumber(number);
         case Leniency.VALID:
             {
                 if (!util.IsValidNumber(number) ||
                     !PhoneNumberMatcher.ContainsOnlyValidXChars(number, candidate, util))
                     return false;
                 return PhoneNumberMatcher.IsNationalPrefixPresentIfRequired(number, util);
             }
         case Leniency.STRICT_GROUPING:
             {
                 if (!util.IsValidNumber(number) ||
                    !PhoneNumberMatcher.ContainsOnlyValidXChars(number, candidate, util) ||
                    PhoneNumberMatcher.ContainsMoreThanOneSlash(candidate) ||
                    !PhoneNumberMatcher.IsNationalPrefixPresentIfRequired(number, util))
                 {
                     return false;
                 }
                 return PhoneNumberMatcher.CheckNumberGroupingIsValid(
                     number, candidate, util, (PhoneNumberUtil u, PhoneNumber n,
                                  StringBuilder nc,
                                  String[] eg) =>
                         {
                             return PhoneNumberMatcher.AllNumberGroupsRemainGrouped(u, n, nc, eg);
                         }
                     );
             }
         case Leniency.EXACT_GROUPING:
         default:
             {
                 if (!util.IsValidNumber(number) ||
                         !PhoneNumberMatcher.ContainsOnlyValidXChars(number, candidate, util) ||
                         PhoneNumberMatcher.ContainsMoreThanOneSlash(candidate) ||
                         !PhoneNumberMatcher.IsNationalPrefixPresentIfRequired(number, util))
                 {
                     return false;
                 }
                 return PhoneNumberMatcher.CheckNumberGroupingIsValid(
                     number, candidate, util, (PhoneNumberUtil u, PhoneNumber n,
                             StringBuilder normalizedCandidate,
                             String[] expectedNumberGroups) =>
                         {
                             return PhoneNumberMatcher.AllNumberGroupsAreExactlyPresent(
                                 u, n, normalizedCandidate, expectedNumberGroups);
                         }
                     );
             }
     }
 }
 public bool Verify(Leniency leniency, PhoneNumber number, String candidate, PhoneNumberUtil util)
 {
     switch (leniency)
     {
         case Leniency.POSSIBLE:
             return IsPossibleNumber(number);
         case Leniency.VALID:
             {
                 if (!util.IsValidNumber(number) ||
                     !PhoneNumberUtil.ContainsOnlyValidXChars(number, candidate, util))
                     return false;
                 return PhoneNumberUtil.IsNationalPrefixPresentIfRequired(number, util);
             }
         case Leniency.STRICT_GROUPING:
             {
                 if (!util.IsValidNumber(number) ||
                    !PhoneNumberUtil.ContainsOnlyValidXChars(number, candidate, util) ||
                    PhoneNumberUtil.ContainsMoreThanOneSlash(candidate) ||
                    !PhoneNumberUtil.IsNationalPrefixPresentIfRequired(number, util))
                 {
                     return false;
                 }
                 // TODO: Evaluate how this works for other locales (testing has been
                 // limited to NANPA regions) and optimise if necessary.
                 String[] formattedNumberGroups = PhoneNumberUtil.GetNationalNumberGroups(util, number);
                 StringBuilder normalizedCandidate = PhoneNumberUtil.NormalizeDigits(candidate,
                                                                     true /* keep strip non-digits */);
                 int fromIndex = 0;
                 // Check each group of consecutive digits are not broken into separate groups in the
                 // {@code candidate} string.
                 for (int i = 0; i < formattedNumberGroups.Length; i++)
                 {
                     // Fails if the substring of {@code candidate} starting from {@code fromIndex} doesn't
                     // contain the consecutive digits in formattedNumberGroups[i].
                     fromIndex = normalizedCandidate.ToString().IndexOf(formattedNumberGroups[i], fromIndex);
                     if (fromIndex < 0)
                     {
                         return false;
                     }
                     // Moves {@code fromIndex} forward.
                     fromIndex += formattedNumberGroups[i].Length;
                     if (i == 0 && fromIndex < normalizedCandidate.Length)
                     {
                         // We are at the position right after the NDC.
                         if (char.IsDigit(normalizedCandidate[fromIndex]))
                         {
                             // This means there is no formatting symbol after the NDC. In this case, we only
                             // accept the number if there is no formatting symbol at all in the number, except
                             // for extensions.
                             String nationalSignificantNumber = util.GetNationalSignificantNumber(number);
                             return normalizedCandidate.ToString().Substring(fromIndex - formattedNumberGroups[i].Length)
                                 .StartsWith(nationalSignificantNumber);
                         }
                     }
                 }
                 // The check here makes sure that we haven't mistakenly already used the extension to
                 // match the last group of the subscriber number. Note the extension cannot have
                 // formatting in-between digits.
                 return normalizedCandidate.ToString().Substring(fromIndex).Contains(number.Extension);
             }
         case Leniency.EXACT_GROUPING:
         default:
             {
                 if (!util.IsValidNumber(number) ||
                         !ContainsOnlyValidXChars(number, candidate, util) ||
                         ContainsMoreThanOneSlash(candidate) ||
                         !IsNationalPrefixPresentIfRequired(number, util))
                 {
                     return false;
                 }
                 // TODO: Evaluate how this works for other locales (testing has been
                 // limited to NANPA regions) and optimise if necessary.
                 StringBuilder normalizedCandidate = PhoneNumberUtil.NormalizeDigits(candidate,
                                                                     true /* keep strip non-digits */);
                 String[] candidateGroups =
                     NON_DIGITS_PATTERN.Split(normalizedCandidate.ToString());
                 // Set this to the last group, skipping it if the number has an extension.
                 int candidateNumberGroupIndex =
                     number.HasExtension ? candidateGroups.Length - 2 : candidateGroups.Length - 1;
                 // First we check if the national significant number is formatted as a block.
                 // We use contains and not equals, since the national significant number may be present with
                 // a prefix such as a national number prefix, or the country code itself.
                 if (candidateGroups.Length == 1 ||
                     candidateGroups[candidateNumberGroupIndex].Contains(
                         util.GetNationalSignificantNumber(number)))
                 {
                     return true;
                 }
                 String[] formattedNumberGroups = PhoneNumberUtil.GetNationalNumberGroups(util, number);
                 // Starting from the end, go through in reverse, excluding the first group, and check the
                 // candidate and number groups are the same.
                 for (int formattedNumberGroupIndex = (formattedNumberGroups.Length - 1);
                      formattedNumberGroupIndex > 0 && candidateNumberGroupIndex >= 0;
                      formattedNumberGroupIndex--, candidateNumberGroupIndex--)
                 {
                     if (!candidateGroups[candidateNumberGroupIndex].Equals(
                         formattedNumberGroups[formattedNumberGroupIndex]))
                     {
                         return false;
                     }
                 }
                 // Now check the first group. There may be a national prefix at the start, so we only check
                 // that the candidate group ends with the formatted number group.
                 return (candidateNumberGroupIndex >= 0 &&
                         candidateGroups[candidateNumberGroupIndex].EndsWith(formattedNumberGroups[0]));
             }
     }
 }