English 中文(简体)
如果高压式编码模式发生动态变化,如何在java验证高压法?
原标题:How to validate zip code in java if the zip code pattern changes dynamically?

我需要验证 j的zi。 我 go,经常说几句。

我面临的问题是,从某些行政环境页起,可能需要的手法模式可能发生变化。 和在某些情况下一样,我们可能需要用户以三十-三十格式加入(XXXXXX) 或可能是(XXXXXX) 等。

在某些情况下,血压法只能包括数字,有时是甲型数字。

请提供帮助。

最佳回答

不幸的是,对邮政地址,包括邮政代码(aka zip代码)没有良好的本地化支持。 这个博客员额(大约一年,但不幸仍然很相关)描述了邮政地址验证的令人不安的状况。 在美国,邮政局提供;Address Validation/59/;网上协议。 我知道的唯一其他工具是一个叫做libaddressinput。 项目摘要指出,虽然统一调查是针对具体的,但后端可以再使用。

问题回答

由于这方面没有巨大的工具,这里有一些法典规定,在不使用管制法的情况下,对编码适用某些模式。 你们可以有你保护伞提供的模式,但可以清楚地看到。

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;
}

为了允许字母数字,您可修改<代码>Character.isDigit至Character.isLetterOrDigit。 然而,如果不同国家有不同的制约因素(它们确实有这些限制),这种限制将难以想象。

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签