You are here

function us_formatter_default in Phone Number 6

Same name and namespace in other branches
  1. 7 includes/phone.us.inc \us_formatter_default()

Default formatter for international phone number.

Parameters

$element: $element['#item']['country_codes']: alpha-2 country code $element['#item']['number']: phone number

$error: The error message to shown to user. Available parameters to use in the error message are

  • "%countrycode": the alpha-2 CC
  • "%phone_input": the original number input by user (could be invalid)
  • "%max_length": allowed maximum length of the phone number

Return value

boolean TRUE if it is a valid phone number for that country, FALSE otherwise.

1 call to us_formatter_default()
ca_formatter_default in includes/phone.ca.inc
Default formatter for international phone number.

File

includes/phone.us.inc, line 82
CCK Field for North American phone numbers.

Code

function us_formatter_default($element) {
  $item = $element['#item'];
  $phone = '';

  // Display a global phone number with country code.
  $cc = cck_phone_countrycodes($item['country_codes']);

  // Format the phone number however you like, this is the default
  // define regular expression
  $regex = '/^
    ([2-9][0-8]\\d)  # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit)
    ([2-9]\\d{2})    # 3-digit prefix (cannot start with 0 or 1)
    (\\d{4})         # 4-digit line number
    /x';
  $result = preg_match($regex, $item['number'], $matches);
  if ($result) {

    // output as +1 (AAA) BBB CCCC
    $phone = $cc['code'] . ' (' . $matches[1] . ') ' . $matches[2] . '-' . $matches[3];
  }
  return $phone;
}