Validate Zip code using JavaScript Regular expression
Continuing with my series of JavaScript regular expression today we will see how easy it is to validate zip code using regular expression in JavaScript. Previously we talked about validating email and Social Security number using JS regex.
In today’s post, I’ll show you how to use JavaScript regular expression to validate zip code. Let me first show you the JS code.
function validateZipCode(elementValue){ var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/; return zipCodePattern.test(elementValue); }
Explanation:
The argument to this method is the zip code you want to validate.
In the method body we define a variable (‘zipCodePattern’) and assign a regular expression to it.
ZipCode format: The regular expression for zip code is
/^\d{5}$|^\d{5}-\d{4}$/
This is quite a simple regular expression.
What we are saying here is that a valid zip code can either have 5 digits or 5 digits followed by an hyphen(-) and ends with four digits. So, zip codes 38980 and 83900-8789 will pass validation. However, 83900- or 839008789 will not pass our validation test.
If you don’t want to have a zip+4 format you can use /^\d{5}$/ as the regular expression to validate simple zip code.
Image credit:smcgee
Trevor
May 30, 2009 @ 3:47 pm
Very helpful code, thanks!
Validate U.S Phone Numbers using JavaScript Regular expression.
February 13, 2011 @ 9:38 pm
[…] validate U.S phone number. Previously we talked about validating email , Social Security number and zip code using JS […]
Eddie
April 28, 2011 @ 5:09 am
Precisely what I needed!! Thanks a bunch…
sachin
July 11, 2011 @ 3:23 am
Exactly what i m needed , thanks a ton
metformin online
October 1, 2011 @ 11:01 pm
Great post I must say. Simple but yet interesting. Wonderful work!
noushad
November 11, 2011 @ 2:15 am
simple and good
kedar
December 27, 2011 @ 7:55 am
thanks yar
Zipcode regex | Mdigspr31
March 31, 2012 @ 1:00 pm
[…] Validate Zip code using JavaScript Regular expressionContinuing with my series of JavaScript regular expression today we will see how easy it is to validate zip code using regular expression in JavaScript. […]
Dominick
April 25, 2012 @ 6:16 pm
this should also work if you want it even more succinct!
var zipCodePattern = /^d{5}(-d{4})?$/;
for those that are new to regex, the ‘?’ means “match either 0 or 1 of the thing preceding it,” which, in this case, is the optional dash and four digits
Supreme Search PPC
September 9, 2014 @ 4:57 am
Hey there! Quick question that’s entirely off topic.
Do you know how to make your site mobile friendly? My web
site looks weird when browsing from my iphone.
I’m trying to find a template or plugin that might be able to
fix this issue. If you have any recommendations, please share.
Cheers!
Maybell
September 10, 2014 @ 2:52 am
This means that many coaches aare still operating as if the training methods used in the Soviet Union in the 1970’s
are state of the art. That inquiry could be expanded because
in that location are no longer gratuitous online football games do work.
At the same time, it also allows great control over the team and individual players throughout the match.
Also visit my blog … Cheats Topp Eleven (Maybell)
American Classifieds
February 8, 2015 @ 9:40 pm
Brilliant coding.
Sim Fote
September 22, 2017 @ 8:58 am
Good starting point. I was wondering how you can modify the code to validate zipcodes within a state, so that any zip code outside the state is considered invalid. Any ideas will help.