private bool MatchesEmergencyNumberHelper(String number, String regionCode, bool allowPrefixMatch) { number = PhoneNumberUtil.ExtractPossibleNumber(number); if (PhoneNumberUtil.PlusCharsPattern.MatchBeginning(number).Success) { // Returns false if the number starts with a plus sign. We don't believe dialing the country // code before emergency numbers (e.g. +1911) works, but later, if that proves to work, we can // add additional logic here to handle it. return(false); } var metadata = phoneUtil.GetMetadataForRegion(regionCode); if (metadata == null || !metadata.HasEmergency) { return(false); } var emergencyNumberPattern = new PhoneRegex(metadata.Emergency.NationalNumberPattern); var normalizedNumber = PhoneNumberUtil.NormalizeDigitsOnly(number); // In Brazil, it is impossible to append additional digits to an emergency number to dial the // number. return((!allowPrefixMatch || regionCode.Equals("BR")) ? emergencyNumberPattern.MatchAll(normalizedNumber).Success : emergencyNumberPattern.MatchBeginning(normalizedNumber).Success); }
//phonelib calls public ParsedPhoneNumber IsPhoneNumberValid(string phoneNumber, string defaultIsoCode) { var parsedNumber = new ParsedPhoneNumber(); if (!string.IsNullOrEmpty(phoneNumber)) { var phoneNumberUtil = PhoneNumberUtil.GetInstance(); parsedNumber.IsViablePhoneNumber = PhoneNumberUtil.IsViablePhoneNumber(phoneNumber); parsedNumber.ExtractPossibleNumber = PhoneNumberUtil.ExtractPossibleNumber(phoneNumber); parsedNumber.NormalizedNumber = PhoneNumberUtil.Normalize(phoneNumber); parsedNumber.NormalizedDigitsOnly = PhoneNumberUtil.NormalizeDigitsOnly(phoneNumber); if (parsedNumber.IsViablePhoneNumber.Value) { var numberObject = phoneNumberUtil.Parse(phoneNumber, defaultIsoCode); parsedNumber.E164Format = phoneNumberUtil.Format(numberObject, PhoneNumberFormat.E164); parsedNumber.IntFormat = phoneNumberUtil.Format(numberObject, PhoneNumberFormat.INTERNATIONAL); parsedNumber.IsValidNumber = phoneNumberUtil.IsValidNumber(numberObject); parsedNumber.CountryCode = numberObject.CountryCode; parsedNumber.HasCountryCode = numberObject.HasCountryCode; parsedNumber.PreferredDomesticCarrierCode = numberObject.PreferredDomesticCarrierCode; var geocoder = PhoneNumbers.PhoneNumberOfflineGeocoder.GetInstance(); parsedNumber.GeoCoderDescription = geocoder.GetDescriptionForNumber(numberObject, PhoneNumbers.Locale.English); parsedNumber.NumberType = phoneNumberUtil.GetNumberType(numberObject).ToString(); } } return(parsedNumber); }
private bool MatchesEmergencyNumberHelper(string number, string regionCode, bool allowPrefixMatch) { var possibleNumber = PhoneNumberUtil.ExtractPossibleNumber(number); if (PhoneNumberUtil.PlusCharsPattern.MatchBeginning(possibleNumber).Success) { // Returns false if the number starts with a plus sign. We don't believe dialing the country // code before emergency numbers (e.g. +1911) works, but later, if that proves to work, we can // add additional logic here to handle it. return(false); } var metadata = MetadataManager.GetShortNumberMetadataForRegion(regionCode); if (metadata == null || !metadata.HasEmergency) { return(false); } var normalizedNumber = PhoneNumberUtil.NormalizeDigitsOnly(possibleNumber); var allowPrefixMatchForRegion = allowPrefixMatch && !RegionsWhereEmergencyNumbersMustBeExact.Contains(regionCode); return(matcherApi.MatchNationalNumber(normalizedNumber, metadata.Emergency, allowPrefixMatchForRegion)); }
private static PhoneNumber ExtractPhoneNumber(this string number, string region) { if (string.IsNullOrEmpty(region)) { throw new ArgumentNullException(region); } if (!String.IsNullOrWhiteSpace(number)) { try { var num = PhoneNumberUtil.ExtractPossibleNumber(number); if (!String.IsNullOrWhiteSpace(num)) { var util = PhoneNumberUtil.GetInstance(); var phone = util.Parse(num, region); return(phone); } } catch (Exception e) { } } return(null); }