isPossibleNumberForTypeWithReason method

ValidationResult isPossibleNumberForTypeWithReason(
  1. PhoneNumber number,
  2. PhoneNumberType type
)

Check whether a phone number is a possible number. It provides a more lenient check than isValidNumber in the following sense:

  • It only checks the length of phone numbers. In particular, it doesn't check starting digits of the number.
  • For some numbers (particularly fixed-line), many regions have the concept of area code, which together with subscriber number constitute the national significant number. It is sometimes okay to dial only the subscriber number when dialing in the same area. This function will return [isPossibleLocalOnly] if the subscriber-number-only version is passed in. On the other hand, because isValidNumber validates using information on both starting digits (for fixed line numbers, that would most likely be area codes) and length (obviously includes the length of area codes for fixed line numbers), it will return false for the subscriber-number-only version.

    number the number that needs to be checked type the type we are interested in isPossibleNumberForTypeWithReason returns a ValidationResult object which indicates whether the number is possible

  • Implementation

    ValidationResult isPossibleNumberForTypeWithReason(
        PhoneNumber number, PhoneNumberType type) {
      String nationalNumber = getNationalSignificantNumber(number);
      int countryCode = number.countryCode;
    
      // Note: For regions that share a country calling code, like NANPA numbers,
      // we just use the rules from the default region (US in this case) since the
      // getRegionCodeForNumber will not work if the number is possible but not
      // valid. There is in fact one country calling code (290) where the possible
      // number pattern differs between various regions (Saint Helena and Tristan
      // da Cunha), but this is handled by putting all possible lengths for any
      // country with this country calling code in the metadata for the default
      // region in this case.
      if (!_hasValidCountryCallingCode(countryCode)) {
        return ValidationResult.invalidCountryCode;
      }
    
      String regionCode = getRegionCodeForCountryCode(countryCode);
      // Metadata cannot be null because the country calling code is valid.
      PhoneMetadata metadata =
          _getMetadataForRegionOrCallingCode(countryCode, regionCode)!;
      return _testNumberLength(nationalNumber, metadata, type);
    }