You are here

function valid_int_phone_number in Phone 6

Same name and namespace in other branches
  1. 7 include/phone.int.inc \valid_int_phone_number()

Verifies that $phonenumber is a valid international phone number as per ITU or, if a default country code is specified, a valid subscriber number.

Parameters

$phonenumber: International phone number to validate

Return value

TRUE if valid, FALSE if otherwise.

See also

http://www.itu.int/rec/T-REC-E.123/en

1 call to valid_int_phone_number()
PhoneIntTest::assertConversion in tests/phone.int.test

File

./phone.int.inc, line 14

Code

function valid_int_phone_number($phonenumber) {
  $phonenumber = trim($phonenumber);
  if ($phonenumber === '') {
    return FALSE;
  }
  $phonenumber = _normalize_country_code($phonenumber);
  $base_phonenumber = str_replace(array(
    '.',
    '(',
    ')',
    '[',
    ']',
    '-',
    '+',
    ' ',
  ), '', $phonenumber);
  if (!isset($field['phone_int_max_length'])) {
    $field['phone_int_max_length'] = 15;
  }
  if (strlen($base_phonenumber) > $field['phone_int_max_length']) {
    $error = t('Invalid international phone number: Phone number is too long; international phone numbers are limited to 15 digits.');
    return FALSE;
  }

  // Check if digits are used in the base_phonenumber
  if (!ctype_digit($base_phonenumber)) {
    $error = t('Invalid international phone number: Phone number contains invalid characters; only allowed characters are numbers and punctuation.');
    return FALSE;
  }

  // Extract country code and see if it's correct:
  preg_match('/^\\+(\\d+)/', $phonenumber, $matches);
  $cc = $matches[1];
  if (strlen($cc) > 3) {
    $error = array(
      t('Invalid international phone number: Country code "+%cc" is too long; valid country codes are three digits or less.'),
      array(
        '%cc' => $cc,
      ),
    );
    return FALSE;
  }

  //drupal_set_message('langue cc = ' . $cc, 'error');

  // TODO: Check if parentheses/brackets add up.
  // TODO: Validate the number against the country rules.
  // For now, validate only against a limited number of countries.
  $countrycode = phone_country_code_convert($cc);

  //drupal_set_message('langue countrycode = ' . $countrycode, 'error');
  if (!empty($countrycode)) {
    $valid_phone_function = 'valid_' . $countrycode . '_phone_number';
    module_load_include('inc', 'phone', 'phone.' . $countrycode);
    if (function_exists($valid_phone_function)) {
      return $valid_phone_function($phonenumber, $field);
    }
    else {
      return TRUE;
    }
  }
  return FALSE;
}