Permalink
Browse files

Tighten Canadian postal code validation.

D, F, I, O, Q, U should not be valid anywhere in a canadian postal code.

Fixes #3708
  • Loading branch information...
1 parent 3f94d1d commit 83de70efcf2f4f0f65bb0cf513f7e73927947bea @markstory markstory committed Mar 17, 2013
Showing with 7 additions and 1 deletion.
  1. +4 −0 lib/Cake/Test/Case/Utility/ValidationTest.php
  2. +3 −1 lib/Cake/Utility/Validation.php
@@ -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'));
@@ -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':

11 comments on commit 83de70e

Member

dereuromark replied Mar 18, 2013

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

Member

ceeram replied Mar 18, 2013

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

Member

dereuromark replied Mar 18, 2013

I agree.

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

Member

ADmad replied Mar 18, 2013

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

Owner

markstory replied Mar 18, 2013

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.

Member

dereuromark replied Mar 18, 2013

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

Owner

markstory replied Mar 19, 2013

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

Member

dereuromark replied Mar 19, 2013

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.

Member

ADmad replied Mar 19, 2013

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.

Member

dereuromark replied Mar 19, 2013

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

Owner

markstory replied Mar 19, 2013

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.