Permalink
Browse files

Updating the phone validation routine.

According to NANPA, there are area codes and exchanges that are invalid or reserved and not available for assignment. This would make those phone numbers invalid. For example:

- area-codes: 555, N11, N9X, 37X, 96X
- exchanges: N11
- numbers: 555-0100 thru 555-0199

You could also say 555-1212 is invalid as it is assigned as information for the US.

I changed the tests where 555 as an area code was being used as the assertion will give it a false on the area code and not the exchange as intended.
  • Loading branch information...
cdburgess committed Jul 9, 2013
1 parent 4ded269 commit fa3d4a0bb5a25c461b4a42a4dbaf9829ee1afd93
Showing with 23 additions and 3 deletions.
  1. +22 −2 lib/Cake/Test/Case/Utility/ValidationTest.php
  2. +1 −1 lib/Cake/Utility/Validation.php
@@ -2129,8 +2129,28 @@ public function testPhone() {
$this->assertFalse(Validation::phone('(055) 999-9999'));
$this->assertFalse(Validation::phone('(155) 999-9999'));
$this->assertFalse(Validation::phone('(595) 999-9999'));
- $this->assertFalse(Validation::phone('(555) 099-9999'));
- $this->assertFalse(Validation::phone('(555) 199-9999'));
+ $this->assertFalse(Validation::phone('(213) 099-9999'));
+ $this->assertFalse(Validation::phone('(213) 199-9999'));
+
+ // 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
+ $this->assertFalse(Validation::phone('1-(222)-511-9999'));
+
+ // invalid phone number
+ $this->assertFalse(Validation::phone('1-(222)-555-0199'));
+ $this->assertFalse(Validation::phone('1-(222)-555-0122'));
+
+ // valid phone numbers
+ $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('1 (222) 333 4444'));
$this->assertTrue(Validation::phone('+1 (222) 333 4444'));
@@ -620,7 +620,7 @@ 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)?[-. ]?\\(?[2-9][0-8][0-9]\\)?[-. ]?[2-9][0-9]{2}[-. ]?[0-9]{4}$/';
+ $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*\)|(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*)?)(?!(555(?:\s*(?:[.|\-|\s]\s*))(01([0-9][0-9])|1212)))(?!(555(01([0-9][0-9])|1212)))([2-9]1[02-9]|[2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/';
break;
}
}

0 comments on commit fa3d4a0

Please sign in to comment.