Merge pull request #10311 from jameztrue/master

Post code validation for Canada
This commit is contained in:
Mike Jolley 2016-02-10 13:07:17 +00:00
commit 9252c3f0b8
2 changed files with 15 additions and 1 deletions

View File

@ -70,6 +70,10 @@ class WC_Validation {
case 'US' :
$valid = (bool) preg_match( '/^([0-9]{5})(-[0-9]{4})?$/i', $postcode );
break;
case 'CA' :
// CA Postal codes cannot contain D,F,I,O,Q,U and cannot start with W or Z. https://en.wikipedia.org/wiki/Postal_codes_in_Canada#Number_of_possible_postal_codes
$valid = (bool) preg_match( '/^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])([\ ])?(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$/i', $postcode );
break;
default :
$valid = true;

View File

@ -83,7 +83,17 @@ class Validation extends \WC_Unit_Test_Case {
array( false, \WC_Validation::is_postcode( '99999-ABC', 'BR' ) )
);
return array_merge( $generic, $gb, $us, $ch, $br );
$ca = array(
array( true, \WC_Validation::is_postcode( 'A9A 9A9', 'CA' ) ),
array( true, \WC_Validation::is_postcode( 'A9A9A9', 'CA' ) ),
array( true, \WC_Validation::is_postcode( 'a9a9a9', 'CA' ) ),
array( false, \WC_Validation::is_postcode( 'D0A 9A9', 'CA' ) ),
array( false, \WC_Validation::is_postcode( '99999', 'CA' ) ),
array( false, \WC_Validation::is_postcode( 'ABC999', 'CA' ) ),
array( false, \WC_Validation::is_postcode( '0A0A0A', 'CA' ) )
);
return array_merge( $generic, $gb, $us, $ch, $br, $ca );
}
/**