diff --git a/classes/Country.php b/classes/Country.php index dd79ae914..c5b082620 100644 --- a/classes/Country.php +++ b/classes/Country.php @@ -373,6 +373,10 @@ class CountryCore extends ObjectModel */ public function checkZipCode($zip_code) { + if (empty($this->zip_code_format)) { + return true; + } + $zip_regexp = '/^'.$this->zip_code_format.'$/ui'; $zip_regexp = str_replace(' ', '( |)', $zip_regexp); $zip_regexp = str_replace('-', '(-|)', $zip_regexp); @@ -380,7 +384,7 @@ class CountryCore extends ObjectModel $zip_regexp = str_replace('L', '[a-zA-Z]', $zip_regexp); $zip_regexp = str_replace('C', $this->iso_code, $zip_regexp); - return (bool)preg_match($zip_regexp, (string) $zip_code); + return (bool)preg_match($zip_regexp, $zip_code); } public static function addModuleRestrictions(array $shops = array(), array $countries = array(), array $modules = array()) diff --git a/controllers/front/AuthController.php b/controllers/front/AuthController.php index 41258d478..eee4a0616 100644 --- a/controllers/front/AuthController.php +++ b/controllers/front/AuthController.php @@ -548,10 +548,10 @@ class AuthControllerCore extends FrontController $postcode = $$addresses_type->postcode; /* Check zip code format */ - if ($country->zip_code_format && !$country->checkZipCode($postcode)) { - $this->errors[] = sprintf(Tools::displayError('The Zip/Postal code you\'ve entered is invalid. It must follow this format: %s'), str_replace('C', $country->iso_code, str_replace('N', '0', str_replace('L', 'A', $country->zip_code_format)))); - } elseif (empty($postcode) && $country->need_zip_code) { + if (empty($postcode) && $country->need_zip_code) { $this->errors[] = Tools::displayError('A Zip / Postal code is required.'); + } elseif ($postcode && $country->zip_code_format && !$country->checkZipCode($postcode)) { + $this->errors[] = sprintf(Tools::displayError('The Zip/Postal code you\'ve entered is invalid. It must follow this format: %s'), str_replace('C', $country->iso_code, str_replace('N', '0', str_replace('L', 'A', $country->zip_code_format)))); } elseif ($postcode && !Validate::isPostCode($postcode)) { $this->errors[] = Tools::displayError('The Zip / Postal code is invalid.'); }