Skip to content
Permalink
Browse files

Simplify & fix phone number validation.

The changes made in fa3d4a0 tightened
the area code validation too much and excluded at least one valid area
code. This change replaces that with a more generic solution that does
not attempt to exclude non-allocated but potentially valid area codes.

Fixes #4066
  • Loading branch information...
markstory committed Sep 14, 2013
1 parent 2ec54fb commit 7379d32b596e125fdfb340396bf658c65194ba49
Showing with 14 additions and 6 deletions.
  1. +3 −3 lib/Cake/Test/Case/Utility/ValidationTest.php
  2. +11 −3 lib/Cake/Utility/Validation.php
@@ -2134,9 +2134,6 @@ public function testPhone() {
// invalid area-codes
$this->assertFalse(Validation::phone('1-(511)-999-9999'));
$this->assertFalse(Validation::phone('1-(379)-999-9999'));
$this->assertFalse(Validation::phone('1-(962)-999-9999'));
$this->assertFalse(Validation::phone('1-(295)-999-9999'));
$this->assertFalse(Validation::phone('1-(555)-999-9999'));
// invalid exhange
@@ -2147,10 +2144,13 @@ public function testPhone() {
$this->assertFalse(Validation::phone('1-(222)-555-0122'));
// valid phone numbers
$this->assertTrue(Validation::phone('416-428-1234'));
$this->assertTrue(Validation::phone('1-(369)-333-4444'));
$this->assertTrue(Validation::phone('1-(973)-333-4444'));
$this->assertTrue(Validation::phone('1-(313)-555-9999'));
$this->assertTrue(Validation::phone('1-(222)-555-0299'));
$this->assertTrue(Validation::phone('508-428-1234'));
$this->assertTrue(Validation::phone('1-(508)-232-9651'));
$this->assertTrue(Validation::phone('1 (222) 333 4444'));
$this->assertTrue(Validation::phone('+1 (222) 333 4444'));
@@ -620,11 +620,19 @@ public static function phone($check, $regex = null, $country = 'all') {
case 'all':
// includes all NANPA members.
// see http://en.wikipedia.org/wiki/North_American_Numbering_Plan#List_of_NANPA_countries_and_territories
$regex = '/^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|3[02-689][0-9]|9[02-57-9][0-9]|[246-8][02-46-8][02-46-9])\s*\)';
$regex .= '|(55[0-46-9]|5[0-46-9][5]|[0-46-9]55|[2-9]1[02-9]|[2-9][02-8]1|[2-46-9][02-46-8][02-46-9]))\s*(?:[.-]\s*)?)';
$regex .= '(?!(555(?:\s*(?:[.|\-|\s]\s*))(01([0-9][0-9])|1212)))';
$regex = '/^(?:(?:\+?1\s*(?:[.-]\s*)?)?';
// Area code 555, X11 is not allowed.
$areaCode = '(?![2-9]11)(?!555)([2-9][0-8][0-9])';
$regex .= '(?:\(\s*' . $areaCode . '\s*\)|' . $areaCode . ')';
$regex .= '\s*(?:[.-]\s*)?)';
// Exchange and 555-XXXX numbers
$regex .= '(?!(555(?:\s*(?:[.\-\s]\s*))(01([0-9][0-9])|1212)))';
$regex .= '(?!(555(01([0-9][0-9])|1212)))';
$regex .= '([2-9]1[02-9]|[2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)';
// Local number and extension
$regex .= '?([0-9]{4})';
$regex .= '(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/';
break;

0 comments on commit 7379d32

Please sign in to comment.
You can’t perform that action at this time.