google / google/libaddressinput

Dutch (NL) postal code regex matches invalid values

Open
#208 2 comments 6 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
624
Forks
109
PR merge metrics
No merged PRs in 30d

Description

Hello there,

Following a discussion at work, I looked into a package used for postal code validation ([axlon/postal-code-validation][1]), which is seeding data from the Google Address Data Service (as pointed out in [axlon/postal-code-validation#27][4]).

After a short discussion, @wotta pointed out that the regex used is invalid. The regex (`\d{4} ?[A-Z]{2}`) allows the following invalid postal codes:

- `0123 AB`, which are invalid since all our postal codes start with 1-9.
- `1111 SA`, `1111 SD` and `1111 SS`, which are banned due to the `SA`, `SD`, and `SS` abbreviations which were used in the Second World War [source][2].

A better regex for Dutch postal codes would be `[1-9]\d{3} ?(?!SA|SD|SS)[A-Z]{2}`, which explicitly excludes zero-based postal codes and invalid suffixes.

I made some tests on [regex101.com][3] (click on "Unit Tests" in the left sidebar).

[1]: https://github.com/axlon/laravel-postal-code-validation
[2]: https://en.wikipedia.org/wiki/Postal_codes_in_the_Netherlands
[3]: https://regex101.com/r/klekDl/1
[4]: https://github.com/axlon/laravel-postal-code-validation/issues/27

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.