Determine the country code a number starts with, or return null if its not a valid country code.
517644
function ParseCountryCode(number) {
    for (var n = 1; n <= 3; ++n) {
      var cc = number.substr(0,n);
      if (dataBase[cc])
        return cc;
    }
    return null;
  }
1143754
public static function validate($phoneNumber, $country = null)
    {
        $phoneNumber = trim($phoneNumber);
        if (empty($phoneNumber)) {
            return false;
        }
        $country = strtoupper($country);
        $phoneUtil = PhoneNumberUtil::getInstance();
        try {
            $numberProto = $phoneUtil->parse($phoneNumber, $country);
        } catch (NumberParseException $e) {
            return false;
        }

        return $phoneUtil->isValidNumber($numberProto);
    }
443194
def _parse_prefix_as_idd(idd_pattern, number):
    
    match = idd_pattern.match(number)
    if match:
        match_end = match.end()
        # Only strip this if the first digit after the match is not a 0, since
        # country calling codes cannot begin with 0.
        digit_match = _CAPTURING_DIGIT_PATTERN.search(number[match_end:])
        if digit_match:
            normalized_group = normalize_digits_only(digit_match.group(1))
            if normalized_group == U_ZERO:
                return (False, number)
        return (True, number[match_end:])
    return (False, number)
1257366
public static boolean isSEPACountry(String countryCode) {
        int index = indexOf(countryCode);
        if (index > -1) {
            return (CountryCodes.COUNTRY_IBAN_LENGTHS[index] & SEPA) == SEPA;
        }
        return false;
    }
1156272
def country_or_default_country(country)
      country ||= (original_starts_with_plus? ? nil : Phonelib.default_country)
      country && country.to_s.upcase
    end
371977
function IsNationalNumber(number, md) {
    return IsValidNumber(number, md) && md.nationalPattern.test(number);
  }
784065
function FormatNumber(regionMetaData, number, intl) {
    // We lazily parse the format description in the meta data for the region,
    // so make sure to parse it now if we haven't already done so.
    ParseFormat(regionMetaData);
    var formats = regionMetaData.formats;
    if (!formats) {
      return null;
    }
    for (var n = 0; n < formats.length; ++n) {
      var format = formats[n];
      // The leading digits field is optional. If we don't have it, just
      // use the matching pattern to qualify numbers.
      if (format.leadingDigits && !format.leadingDigits.test(number))
        continue;
      if (!format.pattern.test(number))
        continue;
      if (intl) {
        // If there is no international format, just fall back to the national
        // format.
        var internationalFormat = format.internationalFormat;
        if (!internationalFormat)
          internationalFormat = format.nationalFormat;
        // Some regions have numbers that can't be dialed from outside the
        // country, indicated by "NA" for the international format of that
        // number format pattern.
        if (internationalFormat == "NA")
          return null;
        // Prepend "+" and the country code.
        number = "+" + regionMetaData.countryCode + " " +
                 number.replace(format.pattern, internationalFormat);
      } else {
        number = number.replace(format.pattern, format.nationalFormat);
        // The region has a national prefix formatting rule, and it can be overwritten
        // by each actual number format rule.
        var nationalPrefixFormattingRule = regionMetaData.nationalPrefixFormattingRule;
        if (format.nationalPrefixFormattingRule)
          nationalPrefixFormattingRule = format.nationalPrefixFormattingRule;
        if (nationalPrefixFormattingRule) {
          // The prefix formatting rule contains two magic markers, "$NP" and "$FG".
          // "$NP" will be replaced by the national prefix, and "$FG" with the
          // first group of numbers.
          var match = number.match(SPLIT_FIRST_GROUP);
          if (match) {
            var firstGroup = match[1];
            var rest = match[2];
            var prefix = nationalPrefixFormattingRule;
            prefix = prefix.replace("$NP", regionMetaData.nationalPrefix);
            prefix = prefix.replace("$FG", firstGroup);
            number = prefix + rest;
          }
        }
      }
      return (number == "NA") ? null : number;
    }
    return null;
  }
495412
def validate(value):
        
        if not helpers.has_len(value):
            return False

        return COUNTRIES.has_key(str(value).lower())
12336
function strip_country_phone_code(formatted_number, metadata)
{
	if (!formatted_number || formatted_number[0] !== '+' || formatted_number === '+')
	{
		return formatted_number
	}

	for (const country_phone_code of Object.keys(metadata.country_phone_code_to_countries))
	{
		if (formatted_number.indexOf(country_phone_code) === '+'.length)
		{
			return formatted_number.slice('+'.length + country_phone_code.length).trim()
		}
	}

	return formatted_number
}
167722
public function getCountryCode($country = null)
    {
        if ($country === null) {
            $country = $this->_country;
        }

        $code = KlarnaCountry::getCode($country);
        return (string) $code;
    }
437693
protected function validateCountry()
    {
        $id = $this->getUpdatingId();

        if ($id === false) {
            return null;
        }

        $data = $this->country->get($id);

        if (empty($data)) {
            $this->setErrorUnavailable('update', $this->translation->text('Country'));
            return false;
        }

        $this->setUpdating($data);
        return true;
    }
301744
protected function validateNumericCodeCurrency()
    {
        $field = 'numeric_code';

        if ($this->isExcluded($field)) {
            return null;
        }

        $value = $this->getSubmitted($field);

        if ($this->isUpdating() && !isset($value)) {
            $this->unsetSubmitted($field);
            return null;
        }

        $label = $this->translation->text('Numeric code');

        if (empty($value)) {
            $this->setErrorRequired($field, $label);
            return false;
        }

        if (preg_match('/^[0-9]{3}$/', $value) !== 1) {
            $this->setErrorInvalid($field, $label);
            return false;
        }

        $updating = $this->getUpdating();

        if (isset($updating['numeric_code']) && $updating['numeric_code'] == $value) {
            return true;
        }

        foreach ($this->currency->getList() as $currency) {
            if ($currency['numeric_code'] == $value) {
                $this->setErrorExists($field, $label);
                return false;
            }
        }

        return true;
    }
1060922
private static Object toNumber( String value)
    {
    Object number;

    try
      {
      number = toExternalNumber( new BigDecimal( value));
      }
    catch( Exception e)
      {
      number = null;
      }

    return number;
    }
327318
public static String getCountry(final String localeString) {
        if (localeString.length() >= COUNTRY_END) {
            return localeString.substring(COUNTRY_START, COUNTRY_END);
        }

        return "";
    }
952814
function getExample(countryCode, numberType, national) {
    try {
        var phoneUtil = i18n.phonenumbers.PhoneNumberUtil.getInstance();
        var numberObj = phoneUtil.getExampleNumberForType(countryCode, numberType);
        var format = (national) ? i18n.phonenumbers.PhoneNumberFormat.NATIONAL : i18n.phonenumbers.PhoneNumberFormat.INTERNATIONAL;
        return phoneUtil.format(numberObj, format);
    } catch (e) {
        return "";
    }
}
68665
def country_choices(first=None, default_country_first=True):
    
    locale = _get_locale()
    territories = [
        (code, name) for code, name in locale.territories.items() if len(code) == 2
    ]  # skip 3-digit regions

    if first is None and default_country_first:
        first = default_country()

    def sortkey(item):
        if first is not None and item[0] == first:
            return "0"
        return to_lower_ascii(item[1])

    territories.sort(key=sortkey)
    return territories
620871
public function validateCodeCountryState()
    {
        $field = 'code';

        if ($this->isExcluded($field)) {
            return null;
        }

        $value = $this->getSubmitted($field);

        if ($this->isUpdating() && !isset($value)) {
            $this->unsetSubmitted($field);
            return null;
        }

        $updating = $this->getUpdating();

        if (isset($updating['code']) && $updating['code'] === $value) {
            return true;
        }

        $label = $this->translation->text('Code');

        if (empty($value)) {
            $this->setErrorRequired($field, $label);
            return false;
        }

        $existing = $this->state->getList(array('code' => $value, 'country' => $this->getSubmitted('country')));

        if (!empty($existing)) {
            $this->setErrorExists($field, $label);
            return false;
        }

        return true;
    }