I'm looking for a regex that validates the US Zip Codes and cannot contain more then 2 0's at the start:
Eg: 00979 is a valid zip code (Puerto Rico)
But I want the user to refrain from entering something like 000979 or 00012
Valid Zipcodes are: 979, 0979, 00979, 970, 907, 0907, 00970 ...
Invalid Zipcodes are: 00000, 00012, 12, ...
I've come up with
/^\d[0-9][0-9]{1,3}[1-9]([\-]\d{4})?$/
but this does not validates 979. What am I doing wrong here? I've looked at other references regarding validation US Zip codes but none of them validates trailing 0's cases..
You can use this regex that allows up to 2 zeroes at the start:
^(?!0{3})[0-9]{3,5}$
RegEx Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With