Simplify & fix phone number validation.

The changes made in fa3d4a0bb5 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
This commit is contained in:
mark_story 2013-09-13 23:05:40 -04:00
parent 2ec54fba50
commit 7379d32b59
2 changed files with 14 additions and 6 deletions

View file

@ -2134,9 +2134,6 @@ class ValidationTest extends CakeTestCase {
// 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 @@ class ValidationTest extends CakeTestCase {
$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'));

View file

@ -620,11 +620,19 @@ class Validation {
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;