RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$
Description
This expression matches three different formats of postal codes: 5 digit US ZIP code, 5 digit US ZIP code + 4, and 6 digit alphanumeric Canadian Postal Code. The first one must be 5 numeric digits. The ZIP+4 must be 5 numeric digits, a hyphen, and then 4 numeric digits. The Canadian postal code must be of the form ANA NAN where A is any uppercase alphabetic character and N is a numeric digit from 0 to 9.
Matches
44240 | 44240-5555 | G3H 6A3
Non-Matches
Ohio | abc | g3h6a3
Author Rating: The rating for this expression. Steven Smith
Source
Your Rating
Bad Good

Enter New Comment

Title
 
Name
 
Comment
 
Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: Specifying case
Name: Nick Bezuidenhout
Date: 5/16/2013 4:48:56 AM
Comment:
The regex [A-Z] finds upper and lower case characters. Any ideas on other ways to specify only upper case characters? I had a look at a perl regex website that says \l specifies lower case, but the regex tester on this website does not recognise that escape.


Title: Best Regex for it
Name: Yogeswaran
Date: 1/28/2011 5:45:48 AM
Comment:
$.validator.addMethod('postalCode', function (value) { return /^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$/.test(value); }, 'Please enter a valid US or Canadian postal code.');


Title: Best Regex for it
Name: Yogeswaran
Date: 1/28/2011 5:45:24 AM
Comment:
$.validator.addMethod('postalCode', function (value) { return /^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$/.test(value); }, 'Please enter a valid US or Canadian postal code.');


Title: Regular Expression Details
Name: Prashant
Date: 12/30/2010 8:05:01 AM
Comment:
it's not working fine when we enter the value like 44240 | 44240-5555 | G3H 6A3.It's also dispaly the error message.


Title: Case insensitive
Name: DK
Date: 9/8/2010 12:15:23 PM
Comment:
Altered Andrew's to accept upper or lower case characters. ^[ABCEGHJ-NPRSTVXYabceghj-nprstvxy]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Zabceghj-nprstv-z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Zabceghj-nprstv-z]{1}[0-9]{1}$


Title: Too many letters
Name: Andrew
Date: 5/11/2010 2:08:15 PM
Comment:
Canadian postal codes have a structure that does not allow EVERY alphabetic character. A more correct version is /^[ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[ ]?[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}$/


Title: Seems to allow too many digits
Name: Geoff
Date: 10/29/2009 6:41:38 PM
Comment:
Should't it reject input such as 1234567 ?


Title: Postal Pattern
Name: Graham Salter
Date: 4/9/2009 2:02:18 PM
Comment:
This is upgraded from Steven smiths code. It now accepts upper/lower case space, no space or dash between Canadian postal code. /^\d{5}-\d{4}|\d{5}|[a-zA-Z]\d{1}[a-zA-Z](\-| |)\d{1}[a-zA-Z]\d{1}$/;


Title: Regular exp validation for US/Canadian zip code
Name: Pratik
Date: 6/10/2008 8:05:32 AM
Comment:
Nice Info and really helpful


Title: Regular exp validation for US/Canadian zip code
Name: Raghuram
Date: 2/27/2008 7:10:31 AM
Comment:
^[A-Z]\d[A-Z][ ]\d[A-Z]\d$ Only this part of Reg. Epr is enough and why do we require ^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z][ ]\d[A-Z]\d$ both are working fine but only doubt to find why do we require first char checking


Title: US/CA zip validation
Name: PJ Bijoy
Date: 7/1/2004 5:06:40 PM
Comment:
Update to the script because original does not work in some java script environment ^[0-9]{5}-[0-9]{4}$|^[0-9]{5}$|^[A-Z][0-9][A-Z][ ]?[0-9][A-Z][0-9]$


Title: Sr.
Name: PJ Bijoy
Date: 7/1/2004 4:00:58 PM
Comment:
^[0-9]{5}-[0-9]{4}$|^[0-9]{5}$|^[A-Z][0-9][A-Z]\s?[0-9][A-Z][0-9]$ This works with javascript matching the zip code correctly. The original one allows something like 12345-1 or 12345a


Title: Senior Consultant
Name: Jill
Date: 6/25/2004 11:43:08 AM
Comment:
This format allowed 90103-abcde to pass validation.


Title: Regular exp validation for US/Canadian zip code
Name: Todd Klodnicki
Date: 6/23/2004 10:44:30 AM
Comment:
I like this one, but I had trouble with the Canadian postal code. I had to alter it to this: ^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z][ ]\d[A-Z]\d$ in order to match G3H 6A3 otherwise it didn't accept the space.


Title: PHP validation
Name: Benny Hill
Date: 5/14/2004 12:02:01 PM
Comment:
This does not work in PHP


Title: Regular exp validation for US zip code
Name: Archana
Date: 4/14/2004 4:44:05 AM
Comment:
good info... Very helpful


Copyright © 2001-2014, RegexAdvice.com | ASP.NET Tutorials