Cake\Localized\Validation\CnValidation::phone PHP Method

phone() public static method

It is not a strict rule and should cover almost all legal phone numbers. Some (obviously) illegal phone numbers will pass this test, especially cellphone. Since it will be a hard task to maintain a small cellphone prefix "database", just make it simple for now.
public static phone ( string $check ) : boolean
$check string The value to check.
return boolean Success.
    public static function phone($check)
    {
        // optional nation prefix
        $pattern = '/^(((0086)|(\\+86))-?)?(';
        // 1XXXXXXXXXX cellphone
        $pattern .= '(1\\d{10})';
        // XXX(X)-XXXXXXX(X)(-...) house phone
        $pattern .= '|(\\d{3,4}-\\d{7,8}(-\\d{1,6})?)';
        $pattern .= ')$/';
        return (bool) preg_match($pattern, $check);
    }

Usage Example

Beispiel #1
0
 /**
  * test the phone method of CnValidation
  *
  * @return void
  */
 public function testPhone()
 {
     $this->assertTrue(CnValidation::phone('+86-010-27738066'));
     $this->assertTrue(CnValidation::phone('13901005000'));
     $this->assertTrue(CnValidation::phone('008613901005000'));
     $this->assertTrue(CnValidation::phone('+8613901005000'));
     $this->assertTrue(CnValidation::phone('010-1234567'));
     $this->assertTrue(CnValidation::phone('0591-88888888'));
     $this->assertTrue(CnValidation::phone('010-12345678-123'));
     $this->assertFalse(CnValidation::phone('123123'));
     $this->assertFalse(CnValidation::phone('123123123xx'));
     $this->assertFalse(CnValidation::phone('0591-110'));
     $this->assertFalse(CnValidation::phone('1234567'));
 }