Saturday
19
September
2009

Zend_Validate_PostCode

Did you ever want to know if a given input is a valid postalcode ?
If you did, then there is a solution for you within Zend Framework 1.10.

I added a new component Zend_Validate_PostCode.
Zend_Validate_PostCode is a validator which can automatically detect the postal code of the users country.

It takes the full qualified locale to set the right format:

$valid = new Zend_Validate_PostCode('de_AT');
$valid->isValid('1234'); // returns true

So how does this work. Zend_Validate_PostCode extracts the country from the full qualified locale.
It uses therefor eighter the set application wide locale, the users locale or a given locale.
Zend_Validate_PostCode knows the notation for all existing postal codes from about 150 different countries. It has therefor all postal codes stored as regex.

Note that small locales only hold the language and do not contain any regional informations.

Maybe you would want to accept only partitial postal codes.
In US for example postal codes are detected with this regex:
d{5}([ -]d{4})?

Now you may want to limit the official codes to 5000-5999. You can simply define your own regex:

$valid = new Zend_Validate_PostCode('5d{3}');
$valid->isValid('1234'); // returns false

Another note:
Of course Zend_Validate_PostCode is not able to detect if a given postcode exists or not.
1234 may exist but 1233 not. Zend_Validate_PostCode detects only if the notation is correct. As postcodes change multiple times a year it would be an very expensive task to collect them and keep them up to date.

I hope you find this validator usefull and have use for it.

More to come soon

Greetings
Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top