/**
  * @param exampleNumberRequestedType  type we are requesting an example number for
  * @param possibleExpectedTypes       acceptable types that this number should match, such as
  *     FIXED_LINE and FIXED_LINE_OR_MOBILE for a fixed line example number.
  */
 private void checkNumbersValidAndCorrectType(PhoneNumberType exampleNumberRequestedType,
                                              HashSet <PhoneNumberType> possibleExpectedTypes)
 {
     foreach (var regionCode in phoneNumberUtil.GetSupportedRegions())
     {
         PhoneNumber exampleNumber =
             phoneNumberUtil.GetExampleNumberForType(regionCode, exampleNumberRequestedType);
         if (exampleNumber != null)
         {
             if (!phoneNumberUtil.IsValidNumber(exampleNumber))
             {
                 invalidCases.Add(exampleNumber);
                 //LOGGER.log(Level.SEVERE, "Failed validation for " + exampleNumber.toString());
             }
             else
             {
                 // We know the number is valid, now we check the type.
                 PhoneNumberType exampleNumberType = phoneNumberUtil.GetNumberType(exampleNumber);
                 if (!possibleExpectedTypes.Contains(exampleNumberType))
                 {
                     wrongTypeCases.Add(exampleNumber);
                     //LOGGER.log(Level.SEVERE, "Wrong type for " + exampleNumber.toString() + ": got " + exampleNumberType);
                     //LOGGER.log(Level.WARNING, "Expected types: ");
                     //for (PhoneNumberType type : possibleExpectedTypes) {
                     //LOGGER.log(Level.WARNING, type.toString());
                 }
             }
         }
     }
 }
Example #2
0
        private void ValidateMobilePhoneNumber(string mobilePhoneNumber)
        {
            PhoneNumberUtil  instance = PhoneNumberUtil.GetInstance();
            HashSet <string> validSupportedRegions = this.supportedValidationRegions
                                                     .Join(
                instance.GetSupportedRegions(),
                s => s,
                r => r,
                (s, r) => s)
                                                     .OrderBy(code => code)
                                                     .ToHashSet();

            bool validPhoneNumber = false;

            foreach (string region in validSupportedRegions)
            {
                try
                {
                    PhoneNumber phoneNumber = instance.Parse(mobilePhoneNumber, region);
                    if (instance.GetNumberType(phoneNumber) != PhoneNumberType.MOBILE)
                    {
                        continue;
                    }

                    validPhoneNumber = true;
                    break;
                }
                catch (NumberParseException)
                {
                }
            }

            if (validPhoneNumber)
            {
                return;
            }

            StringBuilder errorMessage = new StringBuilder();

            errorMessage.AppendLine("Invalid mobile phone number.");
            errorMessage.AppendLine("Examples :");
            foreach (string region in validSupportedRegions)
            {
                PhoneNumber example = instance.GetExampleNumberForType(region, PhoneNumberType.MOBILE);

                StringBuilder formattedExample = new StringBuilder();

                if (example.NumberOfLeadingZeros > 0)
                {
                    formattedExample.Append("(");
                    for (int i = 0; i < example.NumberOfLeadingZeros; i++)
                    {
                        formattedExample.Append("0");
                    }

                    formattedExample.Append(")");
                }

                formattedExample.Append(example.NationalNumber);

                errorMessage.AppendLine($"\t{region} : {formattedExample}");
            }

            throw new ApplicationException(errorMessage.ToString());
        }