Skip to content

Commit

Permalink
Tighten Canadian postal code validation.
Browse files Browse the repository at this point in the history
D, F, I, O, Q, U should not be valid anywhere in a canadian postal code.

Fixes #3708
  • Loading branch information
markstory committed Mar 17, 2013
1 parent 3f94d1d commit 83de70e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/Cake/Test/Case/Utility/ValidationTest.php
Expand Up @@ -2126,6 +2126,10 @@ public function testPostal() {
$this->assertFalse(Validation::postal('BAA 0ABC', null, 'ca'));
$this->assertFalse(Validation::postal('B2A AABC', null, 'ca'));
$this->assertFalse(Validation::postal('B2A 2AB', null, 'ca'));
$this->assertFalse(Validation::postal('K1A 1D1', null, 'ca'));
$this->assertFalse(Validation::postal('K1O 1Q1', null, 'ca'));
$this->assertFalse(Validation::postal('A1A 1U1', null, 'ca'));
$this->assertFalse(Validation::postal('A1F 1B1', null, 'ca'));
$this->assertTrue(Validation::postal('X0A 0A2', null, 'ca'));
$this->assertTrue(Validation::postal('G4V 4C3', null, 'ca'));

Expand Down
4 changes: 3 additions & 1 deletion lib/Cake/Utility/Validation.php
Expand Up @@ -646,7 +646,9 @@ public static function postal($check, $regex = null, $country = 'us') {
$regex = '/\\A\\b[A-Z]{1,2}[0-9][A-Z0-9]? [0-9][ABD-HJLNP-UW-Z]{2}\\b\\z/i';
break;
case 'ca':
$regex = '/\\A\\b[ABCEGHJKLMNPRSTVXY][0-9][A-Z] [0-9][A-Z][0-9]\\b\\z/i';
$district = '[ABCEGHJKLMNPRSTVYX]';
$letters = '[ABCEGHJKLMNPRSTVWXYZ]';
$regex = "/\\A\\b{$district}[0-9]{$letters} [0-9]{$letters}[0-9]\\b\\z/i";
break;
case 'it':
case 'de':
Expand Down

11 comments on commit 83de70e

@dereuromark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably also update (the redundant) https://github.com/cakephp/localized

@ceeram
Copy link
Contributor

@ceeram ceeram commented on 83de70e Mar 18, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe all localized validations should be moved to the localized plugin, also i noticed phone() method uses 'can' as country, where postal uses 'ca'

@dereuromark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.

PS: The same "spelling mistake" I noted a few days ago in the core dev channel.

@ADmad
Copy link
Member

@ADmad ADmad commented on 83de70e Mar 18, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also prefer moving all localized stuff into the localized plugin. But we can't break BC in 2.x.

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we started the localized plugin after there were more and more demands for various other locales. That's the main reason the localized hooks exist in the core. I'm fine with extracting the localizations out in a future release. However, which 'postal' implementation is the canonical one? USA is just another locale to me as a non-american.

@dereuromark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like the phone validation there should then be some "all" generic postal code validation regexp allowing "A-Z0-9" and dash + space etc (which would be valid for all countries then).

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except that A-Z0-9 isn't valid in all countries. So the default validation would just be wrong.

@dereuromark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot the missing chars like dash and space [A-Z0-9- ]. Default validation would at least filter always-invalid chars like % and $ etc then.
But I see that this would not be ideal.

@ADmad
Copy link
Member

@ADmad ADmad commented on 83de70e Mar 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such a generic filter wouldn't be useful to anybody. In such a case perhaps the method could simply act as a stub with actual rules plugged in through the localized plugin.

@dereuromark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And throwing an exception maybe if not properly extended by the localized plugin?

@markstory
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A stub method might work, we'd need to be better about keeping localized up to date though. I haven't looked at it recently and don't know how complete/well tested it is.

Please sign in to comment.