Skip to content

Wrong regex for french postal code in isPostalCode #2478

Closed
@Maze-fr

Description

@Maze-fr

Describe the bug
When checking for french postal code with isPostalCode(..., 'FR'), I can use wrong values and get true.

Examples
No department number 96 in France, isPostalCode('96000', 'FR') is true but should be false.
The actual regex is : /^\d{2}\s?\d{3}$/.
Beside, we never use space in postal codes (never seen that in 45 years).

Correction
The basic rules is : department number (2-3 digits) + digits to have up to 5 digits length, and can start with 0 (see here).
The complete list of departments is available here.
Then, the a basic correct regex should be : /^(?:(?:0[1-9]|[1-8]\d|9[0-5])\d{3}|97[1-46]\d{2})$/.
It doesn't take into account some historical specific rules on the last 3 digits, but it's way better than the current one.

Additional context
Validator.js version: 13.12.0
Node.js version: 20.17.0
OS platform: Ubuntu 24.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions