PHP: Form validation, Canadian Postal Code

Validating a Canadian postal code in PHP can be done with a simple Regular Expression. PHP’s preg_match function will return 0 (no match) or 1 (if there are 1 or more matches). To validate a Canadian postal code, you can use the following:

$value = 'A1B 2C3'; // Postal Code to validate
preg_match("/^[a-zA-Z][0-9][a-zA-Z] ?[0-9][a-zA-Z][0-9]$/", $value);

Note that the letters may be upper case or lower case.

More validation routines will follow.

No TweetBacks yet. (Be the first to Tweet this post)
Share this post:
  • Digg
  • Facebook
  • TwitThis
  • del.icio.us
  • Google Bookmarks

2 Responses to “PHP: Form validation, Canadian Postal Code”

Leave a Reply