function us_validate_number in Phone Number 6
Same name and namespace in other branches
- 7 includes/phone.us.inc \us_validate_number()
Verifies that $number is a valid ten-digit North American phone number.
Parameters
$number: Digits only value.
$ext: Digits only value.
$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.
2 calls to us_validate_number()
- ca_validate_number in includes/
phone.ca.inc - Verifies that $number is a valid ten-digit North American phone number.
- USPhoneNumberTest::testPhoneNumber in tests/
phone.us.test
File
- includes/
phone.us.inc, line 24 - CCK Field for North American phone numbers.
Code
function us_validate_number($number, $ext = '', &$error) {
// Don't need to check for extension because it has been checked by generic validation as all digits, unless has special format/requirements
// We don't want to worry about separators
$number = cck_phone_clean_number($number);
// define regular expression
$regex = '/^
([1]*) # an optional 1
[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, $number, $matches);
if ($result && $matches[1] == '') {
return TRUE;
}
elseif ($result && $matches[1] == '1') {
// t() is no needed
$error = 'Please enter a 10 digit North American phone number like "999 999 9999", without the country code "1" or "+1"';
return FALSE;
}
else {
// t() is no needed
$error = '"%phone_input" is not a valid North American phone number, it should be a 10 digit number like "999 999 9999"';
return FALSE;
}
}