【问题标题】:post box/bin validation [closed]邮箱/箱验证[关闭]
【发布时间】:2011-10-10 03:58:53
【问题描述】:

我需要为注册页面中的地址字段提供邮政信箱验证。现在我们在 jquery 中有一个正则表达式验证,它有一些限制如下:

  1. 如果给出地址 polo Rd,它会识别 polo 中的“po”并警告错误消息。

所以,我们应该构建一个新的验证,它不应该接受带有值的地址行:

  1. "PO BOX", "PO BIN", "BIN", "P.O BOX", "P.O BIN", "P.O", "PO"
  2. 以上值在任何情况下都可以
  3. 上述单词之前、中间和之后的空格也应该被找到并验证。例如:" P O 1234 " 应经过验证并警告错误消息。
  4. 但是"Polo Rd""Robin Rd""testbintest" 应该被接受为两个地址行中的有效地址。

现在jquery验证的代码是:

jQuery.validator.addMethod("nopobox", function(value, element) {
   return this.optional(element) || ! /(P(OST)?\.?\s*O(FF(ICE)?)?\.?\s*(?<!(BOX)|(BIN)))|(^[^0-9]*((P(OST)?\.?\s*O(FF(ICE)?)?\.?)|(?<!(BOX)|(BIN))))/i.test(value);
}, "");

【问题讨论】:

  • 嗨,任何人都可以解决这个问题吗?请

标签: regex


【解决方案1】:
/**
 * The below are the possible combinations P.O.BOX, PO.BOX, P O BOX, P O B O
 * X, POBOX, PO BOX, P.O.B.O.X
 * 
 * @param input
 * @return true if the input value = pobox else false
 */
public static boolean checkPoBox(String input) {
    boolean poBox = false;
    String inputVal = input;
    try {
        if (input.length() > 4) {
            inputVal = inputVal.replace(".", "").replace(" ", "")
                    .toLowerCase();
            if (inputVal.contains("pobox")
                    || inputVal.equalsIgnoreCase("pobox")) {
                poBox = true;
            }
        }
    } catch (Exception e) {
        log.debug("Exception from checkPoBox");
    }
    return poBox;
}

【讨论】:

    【解决方案2】:

    您的问题的解决方案是在初始 P.O 匹配之后您需要一个空格。这样你就不会匹配以 Po- 开头的地址。然后你还需要用一个平面 BOX 或 BIN 盖住箱子。

    尝试以下方法:

    /^\s*((P(OST)?.?\s*O(FF(ICE)?)?.?\s+(B(IN|OX))?) |B(IN|OX))/i

    有许多不错的工具可以使正则表达式的设计更易于概述。您可以在网上找到这样一种工具:RegExr

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-02
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 2011-07-22
      • 2011-07-06
      • 2012-02-10
      • 2018-12-08
      相关资源
      最近更新 更多