1

I want to validate USA zip codes and I have used these regex:

/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(value); //First
/^\b\d{5}(-\d{4})?\b$/.test(value); //Second
/^\d{5}(-\d{4})?(?!-)$/.test(value); //Third
/^\d{5}(?:[-\s]\d{4})?$/.test(value); //Fourth

I tested here, here, and here that the USA zip codes start at 1001. Apparently, using either of the regex above accepts "00000" because it has 5 digits?

Ultimately, I want it to accept 4 digits zip code that starts in 1001. Is my regex correct?

Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62
  • How about `/^[1-9]` at the beginning, this will not allow to start with zero – Tushar Aug 26 '15 at 06:55
  • 2
    Is regex absolutely necessary here? Cant you just check whether the length of the number is 4 , whether it is a number greater than 1001 ? – Sooraj Aug 26 '15 at 06:55
  • 1
    Theoretically, but if one wanted to combine this regex with another, one could just concatenate the two rather than portions of code – Dodo Aug 26 '15 at 06:59
  • Please add more examples of valid and invalid ZIP codes. – Wiktor Stribiżew Aug 26 '15 at 07:01
  • 1
    Possible duplicate of [this](http://stackoverflow.com/questions/160550/zip-code-us-postal-code-validation), [this](http://stackoverflow.com/questions/2577236/regex-for-zip-code) and [this](http://stackoverflow.com/questions/578406/what-is-the-ultimate-postal-code-and-zip-regex). – Anders Aug 26 '15 at 07:11
  • 1
    What's wrong with forcing zip codes to be 5 digit by utilizing a leading zero allowing `01001` for Agawam, Massachusetts? In most cases, if someone entered 4 digits, they probably misentered their zip code. You can mention on the side if their zip code is 4 digits, use a leading zero. – Ultimater Aug 26 '15 at 07:14
  • I'd like to point out there's also 3 digit zip codes out there such as `00501` for Holtsville, NY. – Ultimater Aug 26 '15 at 07:18

1 Answers1

1

A ZIP code is not a number. 01001 and 00501 are valid ZIP codes. 1001 and 501 are not valid ZIP codes. A three- or four-digit number is never a valid ZIP code.

It seems like you're trying to solve the problem of validating a ZIP code after its leading zeroes have been dropped because it was converted to a number, when the problem you should be solving is preventing the ZIP code from being converted to a number in the first place. Leading zeroes in a ZIP code are significant, and if some part of your app strips them, that's the part you need to fix. A ZIP code is not a number.

As @Ultimater wrote above, if the user enters fewer than five digits, it should be rejected, with a gentle reminder that they need to enter either five or nine digits. If for some reason part of your app is converting ZIP codes to numbers and you can't fix that part, then when you do validation you should first convert the number to a string and pad it with zeroes until you have five digits. Don't try to make your validation work with three- and four- digit numbers, because those are never valid ZIP codes.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182