You are here

function valid_ca_phone_number in Phone 6

Same name and namespace in other branches
  1. 5 phone.ca.inc \valid_ca_phone_number()
  2. 7 include/phone.ca.inc \valid_ca_phone_number()

Verifies that $phonenumber is a valid ten-digit North American phone number

Parameters

string $phonenumber:

Return value

boolean Returns boolean FALSE if the phone number is not valid.

File

./phone.ca.inc, line 22
CCK Field for Canadian phone numbers.

Code

function valid_ca_phone_number($phonenumber) {
  $phonenumber = trim($phonenumber);

  // define regular expression
  $regex = '/
    \\D*           # ignore non-digits
    (\\d*)         # an optional 1
    \\D*           # optional separator
    [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)
    \\D*           # optional separator
    [2-9]\\d{2}    # 3-digit prefix (cannot start with 0 or 1)
    \\D*           # optional separator
    \\d{4}         # 4-digit line number
    \\D*           # optional separator
    \\d*           # optional extension
    \\D*           # ignore trailing non-digits
    /x';

  // return true if valid, false otherwise
  $result = preg_match($regex, $phonenumber, $matches);
  return $result && ($matches[1] == '' || $matches[1] == '1');
}