libphonenumber\PhoneNumberUtil::isValidNumberForRegion PHP Method

isValidNumberForRegion() public method

Warning: In most cases, you want to use {@link #isValidNumber} instead. For example, this method will mark numbers from British Crown dependencies such as the Isle of Man as invalid for the region "GB" (United Kingdom), since it has its own region code, "IM", which may be undesirable.
public isValidNumberForRegion ( PhoneNumber $number, string $regionCode ) : boolean
$number PhoneNumber the phone number that we want to validate
$regionCode string the region that we want to validate the phone number for
return boolean that indicates whether the number is of a valid pattern
    public function isValidNumberForRegion(PhoneNumber $number, $regionCode)
    {
        $countryCode = $number->getCountryCode();
        $metadata = $this->getMetadataForRegionOrCallingCode($countryCode, $regionCode);
        if ($metadata === null || static::REGION_CODE_FOR_NON_GEO_ENTITY !== $regionCode && $countryCode !== $this->getCountryCodeForValidRegion($regionCode)) {
            // Either the region code was invalid, or the country calling code for this number does not
            // match that of the region code.
            return false;
        }
        $nationalSignificantNumber = $this->getNationalSignificantNumber($number);
        return $this->getNumberTypeHelper($nationalSignificantNumber, $metadata) != PhoneNumberType::UNKNOWN;
    }

Usage Example

コード例 #1
0
 public function testIsValidNumberForRegion()
 {
     $number = "+33 6 76 83 51 85";
     $region = "DE";
     $phoneNumber = $this->phoneUtil->parse($number, $region);
     $this->assertFalse($this->phoneUtil->isValidNumberForRegion($phoneNumber, "DE"));
 }
All Usage Examples Of libphonenumber\PhoneNumberUtil::isValidNumberForRegion
PhoneNumberUtil