Parse() public method

public Parse ( String numberToParse, String defaultRegion ) : PhoneNumber
numberToParse String
defaultRegion String
return PhoneNumber
Esempio n. 1
0
 private static PhoneNumbers.PhoneNumber ParseFromString(PhoneNumbers.PhoneNumberUtil phoneUtil, string numberString, string regionCode)
 {
     try
     {
         return(phoneUtil.Parse(numberString, regionCode));
     }
     catch (NumberParseException)
     {
         return(null);
     }
 }
Esempio 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);
        }
Esempio n. 3
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);
                }
            }
        }