1

我有一个函数,它接受一个字符串并检查它是否包含有效的邮政编码列表:

function ValidateZipCodeString(listOfZipCodes) {
    // Only 5-digit zip codes are allowed
    //  Zip codes can be separated by a comma, a space, or both
    //  Any other characters will cause the error label to display
    var regex = /^\d{5}(\s*,\s*\d{5})*$/;
    return regex.test(listOfZipCodes);
}

一个“有效”的邮政编码字符串可以是

12345,67854
OR
12345 35647, 09873

BUT NOT

1234565
OR
asd45, 12346

这可以正常工作 - 就验证字符串是否正常而言。我现在需要做的是,在任何只有逗号或空格的地方,同时有逗号和空格。所以使用上面的例子,我会得到

12345, 67854
OR
12345, 35647, 09873

我怎样才能做到这一点?

[澄清信息]

我需要澄清我对现有功能进行更改的意图。正如我所说,它工作得很好,我只需要函数传回一个布尔值来告诉调用函数是否显示警告标签。

我需要添加到 ValidateZipCodeString 函数的功能,这样,当字符串中包含有效的邮政编码列表时,我想修改该字符串(确保逗号字符串在每个五位邮政编码之间),然后编写该字符串到邮政编码字符串来自的文本框。

4

3 回答 3

0

我会用两个功能来做到这一点:

s="12345,ad7854"
t="12345 35647, 09873"
function ReformatZipCodeArray(arrayOfZipCodes) {
    // Only 5-digit zip codes are allowed
    //  Zip codes can be separated by a comma, a space, or both
    //  Any other characters will cause the error label to display
    if (arrayOfZipCodes == null) {
        return null;
    }
    var regex = /^\d{5}$/;
    var areZipsInArrayValid = true;
    var invalidZips = [];
    arrayOfZipCodes.map(function(possibleZipCode) {
        if (regex.test(possibleZipCode) == false) {
            areZipsInArrayValid = false;
            invalidZips.push(possibleZipCode);
        }
    });
    if (areZipsInArrayValid) {
        return arrayOfZipCodes.join(", ");
    } else {
        console.log("ERROR: invalid zips are ", invalidZips);
        return null;
    }
}

function split(listOfZipCodes) {
    // Split by either a space or a comma or any combination of those
    if (listOfZipCodes.trim().length == 0) {
        return null;
    }
    var regex = /[\s,]+/;
    return listOfZipCodes.split(regex);
}

console.log(ReformatZipCodeArray(split(s)))

上述代码的输出将如下所示:

ERROR: invalid zips are  ["ad7854"]

有效的 zip 字符串 ("12345 35647, 09873") 的输出如下所示:

12345, 35647, 09873
于 2013-02-28T22:58:52.163 回答
0

我会做:

function ValidateZipCodeString(listOfZipCodes) {
    // Only 5-digit zip codes are allowed
    //  Zip codes can be separated by a comma, a space, or both
    //  Any other characters will cause the error label to display
    var regex = /^\d{5}(?:[\s,]+\d{5})*$/;
    see the change here __^^^^^^
    return regex.test(listOfZipCodes);
}

正则表达式解释:

The regular expression:

(?-imsx:^\d{5}(?:[\s,]+\d{5})*$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  \d{5}                    digits (0-9) (5 times)
----------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    [\s,]+                   any character of: whitespace (\n, \r,
                             \t, \f, and " "), ',' (1 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
    \d{5}                    digits (0-9) (5 times)
----------------------------------------------------------------------
  )*                       end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-03-02T10:26:03.393 回答
0

而不是 \s* 您可以使用逗号和空格的字符类

var regex = /^\d{5}([\s,]*\d{5})*$/;
于 2013-02-28T21:57:46.857 回答