function valid_ca_phone_number in Phone 7
Same name and namespace in other branches
- 5 phone.ca.inc \valid_ca_phone_number()
- 6 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
- include/
phone.ca.inc, line 21 - 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');
}