Form Validation For Zip Code Or Postal Code

Hi guys,

I have a form which shows Zip Code or Postal Code depends on where the user is coming from. I would like to know if Yii has built-in validation for Zip Code or Postal Code.

I am talking about rules declared in rules function in Model. (http://www.yiiframework.com/doc/guide/1.1/en/form.model#declaring-validation-rules)

If there are none built-in zip/postal code, Please guide me on how to create one.

Thank you,

You can use CRegularExpressionValidator.

http://www.yiiframework.com/doc/api/1.1/CRegularExpressionValidator

See the comments at the bottom for the sample codes.

To complete previous post, here is link to regular expressions for USA / Canada zip code validation - http://geekswithblogs.net/MainaD/archive/2007/12/03/117321.aspx

In-case people are looking at how to use these in the context of rules, here you go:

[[‘yourProperty’], ‘match’, ‘pattern’ => ‘yourRegularExpression’]