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. (

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

Thank you,

You can use 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 -

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

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