You are here

function us_formatter_local in Phone Number 6

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

Local formatter for local 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_local()
ca_formatter_local in includes/phone.ca.inc
Local formatter for local phone number.

File

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

Code

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

  // Display a local phone number without country code.
  $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 (AAA) BBB CCCC
    $phone = '(' . $matches[1] . ') ' . $matches[2] . '-' . $matches[3];
  }
  return $phone;
}