IsValidNumberForRegion() public method

public IsValidNumberForRegion ( PhoneNumber number, String regionCode ) : bool
number PhoneNumber
regionCode String
return bool
示例#1
0
        /// <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);
        }
示例#2
0
        /// <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);
        }