Cake\Localized\Validation\DeValidation::postal PHP Method

postal() public static method

Checks a postal code for Germany.
public static postal ( string $check ) : boolean
$check string The value to check.
return boolean Success.
    public static function postal($check)
    {
        $pattern = '/^(0[1-46-9]\\d{3}|[1-357-9]\\d{4}|[4][0-24-9]\\d{3}|[6][013-9]\\d{3})$/';
        return (bool) preg_match($pattern, $check);
    }

Usage Example

Beispiel #1
0
 /**
  * test the postal method of DeValidation
  * according to wikipedia, some combinations are reserved and therefore not valid
  * https://de.wikipedia.org/wiki/Liste_der_Postleitregionen_in_Deutschland#F.C3.BCnfstelliges_System_seit_1993
  * @return void
  */
 public function testPostal()
 {
     $this->assertTrue(DeValidation::postal('51109'));
     // must have 5 digits
     $this->assertFalse(DeValidation::postal('1109'));
     $this->assertFalse(DeValidation::postal('051109'));
     $this->assertFalse(DeValidation::postal('00109'));
     $this->assertTrue(DeValidation::postal('01109'));
     $this->assertFalse(DeValidation::postal('05000'));
     $this->assertTrue(DeValidation::postal('06109'));
     $this->assertFalse(DeValidation::postal('43000'));
     $this->assertTrue(DeValidation::postal('44109'));
     $this->assertFalse(DeValidation::postal('62000'));
     $this->assertTrue(DeValidation::postal('63109'));
 }