3

I need to validate the zip code in java. I have googled and there are many regular expressions.

The problem I have is the required zip code pattern may change from some admin settings page. Like at some instance, we may need users to enter zip code in format XXX - XXX or may be (XXXXXX) etc.

In some case zip code can consist of numbers only and in some cases alpha numeric.

Please help.

ajm
  • 12,863
  • 58
  • 163
  • 234
  • Do you want to be able (through the admin settings page) to define your own zip code formats or select from a list of predefined formats? – Ben van Gompel Nov 21 '11 at 08:57
  • @Ben van Gompel. We will have some predefined zip code formats in the drop down. – ajm Nov 21 '11 at 09:38
  • So, you need to specify a regular expression (or something similar) for each of the options in your drop down and make sure the right one is used for the verification of the zip code. – Ben van Gompel Nov 21 '11 at 11:01
  • Possible duplicate of [What is the ultimate postal code and zip regex?](http://stackoverflow.com/questions/578406/what-is-the-ultimate-postal-code-and-zip-regex) – surhidamatya Nov 05 '15 at 11:51

2 Answers2

1

Unfortunately, there is no good localization support for postal addresses, including postal codes (aka zip codes). This blog post (about a year old, but still, unfortunately, relevant) describes the sorry state of postal address validation. In the U.S., the postal service offers their Address Validation API; a web-based protocol. The only other tool I know of is an Android library called libaddressinput. The project summary says that while the UI is Android-specific, the back end can be reused.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

Since there is no great tool out there, here's some code to apply some patterns to zip codes without the use of regex. You could have the patterns supplied from your servlet, but it's shown in the example for clarity.

Although your question doesn't specify a country, I will post one for US zip codes since those patterns are familiar to me:

public static boolean isProbablyValidUSZipCode(CharSequence zip) {

    String[] patterns = {"#####", "#####-####", "##### ####", "#########"};
    try {
        for (String pattern : patterns) {
            if (checkAgainstPattern(zip, pattern)) {
                return true;
            }
        }
        return false;
    }
    catch (NullPointerException ignored) {
        return false;
    }
}

private static boolean checkAgainstPattern(CharSequence s, CharSequence pattern) {

    if (s.length() != pattern.length()) {
        return false;
    }

    for (int i = 0; i < pattern.length(); i++) {
        char c = s.charAt(i);
        switch (pattern.charAt(i)) {
            case '#':
                if (!Character.isDigit(c)) {
                    return false;
                }
                break;

            default:
                if (c != pattern.charAt(i)) {
                    return false;
                }
        }
    }
    return true;
}

To allow alphanumeric, you can change Character.isDigit to Character.isLetterOrDigit. It will get ugly, however, if different countries have different constraints (which they do).

Of course, this will not do any sort of lookup to catch zip codes that don't exist or are somehow reserved/otherwise invalid, but it's likely better than no validation at all. If you do later find some lookup service, you can always call it after these static methods since I'd imagine that would be more expensive.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80