Skip to content

Commit

Permalink
Add stricter regex for validating French postal codes
Browse files Browse the repository at this point in the history
Updated the postal code validation regex to enforce the specific structure of French postal codes. The new regex ensures that the first two digits fall within the valid ranges (01–95 for metropolitan areas and 96–98 for overseas territories), while still allowing any valid 5-digit code. This improves accuracy over the previous general 5-digit validation, which allowed invalid codes.
  • Loading branch information
johnnyxlemonade committed Sep 28, 2024
1 parent c8c53ee commit 7c11f82
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Formatter/FRFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
*
* Postcodes consist of 5 digits, without separator.
*
* First two digits indicate the department (region)
* 01–95 for metropolitan areas
* 96–98 for overseas territories
*
* @see https://en.wikipedia.org/wiki/List_of_postal_codes
* @see https://en.wikipedia.org/wiki/Postal_codes_in_France
*/
class FRFormatter implements CountryPostcodeFormatter
{

public function format(string $postcode) : ?string
{
if (preg_match('/^[0-9]{5}$/', $postcode) !== 1) {

if (preg_match('/^(?:0[1-9]|[1-8]\d|9[0-8])\d{3}$/', $postcode) !== 1) {
return null;
}

Expand Down

0 comments on commit 7c11f82

Please sign in to comment.