Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This will check for two 5-digit numbers separated by a comma</p> <pre><code>^\d{5},\d{5}$ </code></pre> <p><img src="https://www.debuggex.com/i/2oOo4FRly_CRCGm_.png" alt="Regular expression visualization"></p> <p>But, you said <em>at least</em> two, so that means it needs to be a little more flexible to accommodate more. If the user enters <code>12345,12345,12345</code> it needs to be valid.</p> <pre><code>^\d{5}(?:,\d{5})+$ </code></pre> <p><img src="https://www.debuggex.com/i/e778dnUPFjb4PA2M.png" alt="Regular expression visualization"></p> <p>What if the user adds a space after the comma? Such as <code>12345, 12345</code>. This is perfectly valid, so let's make sure our validator allows that.</p> <pre><code>^\d{5}(?:,\s*\d{5})+$ </code></pre> <p><img src="https://www.debuggex.com/i/SvGUsdfHtpm7Hg9f.png" alt="Regular expression visualization"></p> <p>Oh, and zip codes can have an optional <code>-1234</code> ending on them, too (known as <a href="http://en.wikipedia.org/wiki/ZIP_code#ZIP.2B4" rel="nofollow noreferrer">ZIP+4</a>. Maybe you want something like this</p> <pre><code>^\d{5}(?:-\d{4})?(?:,\s*\d{5}(?:-\d{4})?)+$ </code></pre> <p><img src="https://www.debuggex.com/i/FgU204OOD99wGDXh.png" alt="Regular expression visualization"></p> <p>Now strings like this would be valid</p> <ul> <li>12345</li> <li>12345, 12345,12345</li> <li>12345, 12345-9999, 12345</li> </ul> <hr> <p><strong>As a bonus</strong>, let's say <code>12345, 12345</code> is invalid because it has the same zip code twice. Here's how we'd fix that</p> <pre><code>(?:(\d{5}),?)(?!.*\1) </code></pre> <p><img src="https://www.debuggex.com/i/M7Q8P_4Jt0usdLJg.png" alt="Regular expression visualization"></p> <p>And here's the ZIP+4 version</p> <pre><code>(?:(\d{5}(?:-\d{4})?),?)(?!.*\1(?!-)) </code></pre> <p><img src="https://www.debuggex.com/i/xChMFk5vKbjIxzeP.png" alt="Regular expression visualization"></p> <p>This one has a little added complexity because of possibility of (e.g.,) <code>12345, 12345-9999</code>. This is valid but because <code>12345</code> 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.</p> <p><strong>Note</strong> 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.</p> <pre><code>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; }; </code></pre> <p>And here's the ZIP+4 variant if you want to support that</p> <pre><code>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; }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload