javaformszipcode

What is the min and max character length for a non-US postal code


I am trying to develop an application that accepts a postal code from a form. The form is accessible worldwide. I know that for US zip codes the standard character length is 5, but what is the min and max for non-US postal codes? I need to know because I am using a validation constraint:

/**
     * The zip code used to retrieve a US based customer's state.
     */
    @Nullable
    @Size(min=5, max=5)
    private final String zip;

This question was marked as a duplicate because of this post: I need to store postal codes in a database. How big should the column be?. Which doesn't specify the minimum for all non-US postal codes.


Solution

  • Zipcodes have a lot of complexities. For this reason, they feature in chronicles of programmers' incorrect assumptions.

    You can use guidelines such as this table from FedEx, e.g.

    PT - Portugal - NNNN or NNNN-nnn or NNNNnnn

    But keep in mind that most sources, like that one, will be incomplete.


    I suggest asking: why bother? If you want to accept zip codes from all over the world, and fail if they're not within the range of your allowed length, you're not actually protecting yourself very much at all. A user in a country with the shortest zip codes could enter a zipcode the length of the longest country's. A user in a country with strictly numerical zipcodes might input one with alpha characters.

    The only way to be "sure" (for some meaningful value of sure) is to use some form of address validation.