Skip to content

Commit

Permalink
Updated phone number validation to reflect new mobile operators
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/Validate/trunk@308785 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
CloCkWeRX committed Mar 1, 2011
1 parent 47d310e commit bd741ee
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 44 deletions.
149 changes: 113 additions & 36 deletions Validate/NZ.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,52 +150,129 @@ function region($region)
*/
function phoneNumber($number, $requireAreaCode = true)
{
static $servicePrefix = array("0800", "0900", "0508");
static $mobilePrefix = array("021", "025", "027");
$regexp = '';
$servicePrefix = array("0800", "0900", "0508");

$areaCodes = array("024099", // for the Scott Base in the Ross Dependency
"03", // for the South Island and the Chatham Islands
"04", // for the Wellington Region to Kapiti, but not the Wairarapa and Otaki
"06", // for the he remaining southern and eastern North Island including Taranaki, Manawatu-Wanganui (excluding Taumarunui), Hawke's Bay, Gisborne, the Wairarapa, and Otaki
"07", // for the Waikato, the Bay of Plenty and Taumarunui
"09", // for Auckland and Northland
);

// Remove non-numeric characters that we still accept
$number = str_replace(array("+", " ", "(", ")", "-",), "", trim($number));

// Sanity check
if (!ctype_digit($number)) {
return false;
} else {
$numlength = strlen($number);
switch ($numlength) {
case 7:
case 9:
if (!$requireAreaCode) {
// Is land line w/o area code
$regexp = "(^[0-9]{7}$)";
} else {
// Is land line with area code
$regexp = "(^0(3|4|6|7|9)[0-9]{7}$)";
}
break;
case 10:
if (in_array(substr($number, 0, 4), $servicePrefix)) {
// Is 0800,0900 or 0508 number
$regexp = "(^0(8|9|5)0(0|8)[0-9]{6}$)";
} elseif (in_array(substr($number, 0, 3), $mobilePrefix)) {
//Is Mobile number
$regexp = "(^02(1|5|7)[0-9]{3}[0-9]{4}$)";
}
break;
case 11:
if (substr($number, 0, 4) == '0800') {
// Is 0800 with 7 digits?
$regexp = "(^0800[0-9]{7}$)";
}
}

// http://en.wikipedia.org/wiki/Telephone_numbers_in_New_Zealand#Mobile_Phones
$mobilePrefix = array("orcon" => "020", // Orcon
"vodafone" => "021", // Vodafone; 6-8 digits
"2degrees" => "022", // 2degrees; 7 digits; 2degrees was launched in August 2009.
"telstraClear_unused" => "023", // Unused; Owned by TelstraClear
"expansion" => "024", // Unused; Protected by Management Committee 30.01.09 to preserve the potential code expansion option.
"telecomNZ_deprecated" => "025", // Was used by Telecom New Zealand until it was shut down on 31 March 2007. All numbers have now migrated to 027 (7-digit) and 0274 (6-digit).
"telecomNZ" => "027", // Telecom New Zealand; 7 digits
"compass" => "0280", // Compass Communications
"callPlus" => "028", // CallPlus or BLACK + WHITE
"teletraders" => "0283", // Teletraders MVNO
"m2" => "02885", // M2 MVNO
"airnet" => "02896", // Airnet NZ Ltd
"telstraClear" => "029", // TelstraClear
);

// Is it a mobile number?
foreach ($mobilePrefix as $provider => $prefix) {
// Does it start with a recognised provider prefix?
if (substr($number, 0, strlen($prefix)) != $prefix) {
continue;
}
// Is it the Scott base / Ross Dependency?
if (substr($number, 0, 6) == '024099') {
continue;
}

switch ($provider) {
// Vodafone; 6-8 digits
case 'vodafone':
if (preg_match('/^021[0-9]{6,8}$/', $number)) {
return true;
}
break;
// 2degress; 7 digits
case '2degrees':
if (preg_match('/^022[0-9]{7}$/', $number)) {
return true;
}
break;

// These providers offer numbers with 02, then 7-9 digits
case 'orcon':
case 'compass':
case 'callPlus':
case 'teletraders':
case 'm2':
case 'airnet':
case 'telstraClear':
case 'telecomNZ':
if (preg_match('/^02[0,7,8,9][0-9]{6,8}$/', $number)) {
return true;
}
break;

// These providers aren't valid as of 1/03/2011
case 'expansion':
case 'telstraClear_unused':
case 'telecomNZ_deprecated':
break;
}
}

// Is it a service number of some type?
if (in_array(substr($number, 0, 4), $servicePrefix)) {
// Is 0800,0900 or 0508 number
if (preg_match("(^0(8|9|5)0(0|8)[0-9]{6}$)", $number)) {
return true;
}
}

// Is 0800 with 7 digits?
if (substr($number, 0, 4) == '0800') {
if (preg_match("(^0800[0-9]{7}$)", $number)) {
return true;
}
}

// Is it a general landline of some type?
foreach ($areaCodes as $prefix) {
// Does it start with a recognised area prefix?
if (substr($number, 0, strlen($prefix) != $prefix)) {
continue;
}

if (substr($number, 0, 3) == "640") {
// Is land line with country code
$regexp = "(^640(3|4|6|7|9)[0-9]{7})";
if (!$requireAreaCode) {
// Is land line w/o area code
if (preg_match("(^[0-9]{7}$)", $number)) {
return true;
}
} else {
// Is land line with area code
if (preg_match("(^0(3|4|6|7|9)[0-9]{7}$)", $number)) {
return true;
}
break;
}
}
if ($regexp != '') {
return preg_match($regexp, $number);

// Is land line with country code
if (substr($number, 0, 3) == "640") {
if (preg_match("(^640(3|4|6|7|9)[0-9]{7})", $number)) {
return true;
}
}

return false;
}
/**
Expand Down
9 changes: 4 additions & 5 deletions package_NZ.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ http://pear.php.net/dtd/package-2.0.xsd">
<email>byron.adams54@gmail.com</email>
<active>yes</active>
</lead>
<date>2011-02-23</date>
<date>2011-03-01</date>
<time>23:38:49</time>
<version>
<release>0.1.5</release>
<api>0.1.5</api>
<release>0.1.6</release>
<api>0.1.6</api>
</version>
<stability>
<release>alpha</release>
<api>alpha</api>
</stability>
<license>New BSD</license>
<notes>
Bug #18290 strict post code validation fails
Bug #18291 Lax postcode validation doesn't work
Updated phone number validation to reflect mobile operators; as per http://en.wikipedia.org/wiki/Telephone_numbers_in_New_Zealand#Mobile_Phones
</notes>
<contents>
<dir name="/">
Expand Down
7 changes: 4 additions & 3 deletions tests/Validate_NZ.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ $phoneNumbers = array(


/* Mobile Phones */

"0-21-343 183", // Ok
"0272913164", //Ok
"021 234 5678", //Ok
"027-123-4567", //Ok
"(025) 234-5678", //Ok
"(025) 234-5678", //NOk - shut down on 31 March 2007

/* Must have correct network Prefix (021,027,025) */

Expand Down Expand Up @@ -258,10 +258,11 @@ O1lO: NO
64-03-684-5018: YES
64036845018: YES
64 03 684 5018: YES
0-21-343 183: YES
0272913164: YES
021 234 5678: YES
027-123-4567: YES
(025) 234-5678: YES
(025) 234-5678: NO
0232345678: NO
024-321-8765: NO
1112223456: NO
Expand Down

0 comments on commit bd741ee

Please sign in to comment.