-1

I want to filter multiple zip codes in an input, there should be at least 2 zip SEPARATED by a COMA, I am trying to validate them in javascript with the following code but it's now filtering, the submit send the form to the next page without error, anyone can help?

<script>
function validateMULTIZIP() {
    if(!/\d{11,}/.test(document.zipad.textfield.value) && document.getElementById('single').checked==false))
    {
         alert( "There should be a least two Zip codes separated by a coma." );
         document.zipad.textfield.focus() ;
         return false;
    }
    return true;
}
</script>
4

4 回答 4

6

This will check for two 5-digit numbers separated by a comma

^\d{5},\d{5}$

Regular expression visualization

But, you said at least two, so that means it needs to be a little more flexible to accommodate more. If the user enters 12345,12345,12345 it needs to be valid.

^\d{5}(?:,\d{5})+$

Regular expression visualization

What if the user adds a space after the comma? Such as 12345, 12345. This is perfectly valid, so let's make sure our validator allows that.

^\d{5}(?:,\s*\d{5})+$

Regular expression visualization

Oh, and zip codes can have an optional -1234 ending on them, too (known as ZIP+4. Maybe you want something like this

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

Regular expression visualization

Now strings like this would be valid

  • 12345
  • 12345, 12345,12345
  • 12345, 12345-9999, 12345

As a bonus, let's say 12345, 12345 is invalid because it has the same zip code twice. Here's how we'd fix that

(?:(\d{5}),?)(?!.*\1)

Regular expression visualization

And here's the ZIP+4 version

(?:(\d{5}(?:-\d{4})?),?)(?!.*\1(?!-))

Regular expression visualization

This one has a little added complexity because of possibility of (e.g.,) 12345, 12345-9999. This is valid but because 12345 can appear more than once, it makes sure that a 5-digit zip code can't be invalidated by a unique 9-digit zip code.

Note these duplicate-checking regexps do not enforce the minimum of two unique zip codes. If you want to check for duplicates you'd need to combine the two.

var valid5DigitZipCodes = function(str) {
  if (! /^\d{5}(?:,\s*\d{5})+$/.test(str)) {
    alert("You need at least 2 zip codes");
    return false;
  }

  else if (! /(?:(\d{5}),?)(?!.*\1)/.test(str)) {
    alert("You entered a duplicate zip code");
    return false;
  }

  return true;
};

And here's the ZIP+4 variant if you want to support that

var valid9DigitZipCodes = function(str) {
  if (! /^\d{5}(?:-\d{4})?(?:,\s*\d{5}(?:-\d{4})?)+$/.test(str)) {
    alert("You need at least 2 zip codes");
    return false;
  }

  else if (! /(?:(\d{5}(?:-\d{4})?),?)(?!.*\1(?!-)).test(str) {
    alert("You entered a duplicate zip code");
    return false;
  }

  return true;
};
于 2013-08-26T16:51:43.030 回答
2

假设(根据您的代码)邮政编码包含五位数字且没有其他字符,您可以使用:

/\d{5},\d{5}/.test(document.zipad.textfield.value)

You regex:\d{11,}表示“任何数字,11 次或更多”,这就是它被破坏的原因。

于 2013-08-26T16:52:09.203 回答
0

另一个不使用正则表达式的解决方案是用逗号分割邮政编码,然后检查结果数组的大小。

示例代码:

<input type="text" id="in"></input>
<button onclick="validate()">Click</button>

JS

function validate() {
    var inp = document.getElementById("in");
    var content = inp.value;
    var correct = validateZipString(content);
    if (correct) {
        alert("ok");
    } else {
        alert("not ok");
    }
}

function validateZipString(zipString) {
    var zipCodes = zipString.split(',');
    if (zipCodes.length < 2) return false;
    for (var i = 0; i < zipCodes.length; i++) {
        //validate each zipCode if required
    }
    return true;
}

这是一个工作 jsfiddle http://jsfiddle.net/VcNd9/3/

于 2013-08-26T17:16:20.657 回答
0

对于其他对也匹配 1 个或更多而不是两个或更多的变体感兴趣的人。只需在表达式末尾更改 * 的 + 量词。

从:

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

至:

^\d{5}(?:-\d{4})?(?:,\s*\d{5}(?:-\d{4})?)*$

例如:

<input type="text" inputmode="numeric" pattern="^\d{5}(?:-\d{4})?(?:,\s*\d{5}(?:-\d{4})?)*$">

于 2019-09-27T12:46:39.347 回答