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
  1. bonaparte

    Saturday, September 26, 2009 - 18:49:49

    Useful validator.

    On a side note, what solution do you recommend to convert number to words?

  2. thomasw

    Saturday, October 3, 2009 - 11:40:47

    Actually none… the problem is that words are localized and there is actually no solution to provide numeric words for all languages.

    When you provide these words manually for a single locale you can simply use the Regex or Callback validator.

  3. Alix Axel

    Sunday, December 27, 2009 - 08:29:00

    Hi Thomas, first of all congratulations on your effort.

    I’ve a few question though:

    1) Where can I download Zend_Validate_PostCode? It doesn’t seem to ship with Zend Framework 1.9.
    2) What is the regex to validate pt_PT Postal Codes? d{4}([-]d{3}) or d{3}[05]([-]d{3})?

    Thanks again!

  4. thomasw

    Sunday, December 27, 2009 - 13:54:37

    1.) As you can read within the blog it’s mentioned that “there is a solution for Zend Framework 1.10”.

    This implies that it’s not available for Zend Framework 1.9 in my eyes ;-)

    So you can eighter download the new alpha for ZF 1.10, or wait until ZF 1.10 is shipped (which should be the case within the next 4 weeks in my opinion).

    2.) The official pt_PT postal codes are detected by “d{4}([-]d{3})?”.

    PS: Please don’t ask me the postal codes for all countries… simply try it out or take a look into CLDR/ZF. :-)