472,144 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,144 software developers and data experts.

Regular Expression help

Does anyone know of a regualr expression to validate US and Canadian Postal
Codes?
I want to be able to accept ##### or #####-#### or Canadian A#A #A# that is
alpha,number,alpha number,alpha,number.

I am new to regular expressions. The US 5 digit part is easy, but I get
stuck after that.

var pattern = new RegExp(/\\d{5}/)

Any ideas? Thanks everybody.

Chris
Oct 20 '05 #1
4 1410

Chris Kettenbach wrote:
Does anyone know of a regualr expression to validate US and Canadian Postal
Codes?
I want to be able to accept ##### or #####-#### or Canadian A#A #A# that is
alpha,number,alpha number,alpha,number.

I am new to regular expressions. The US 5 digit part is easy, but I get
stuck after that.

var pattern = new RegExp(/\\d{5}/)

Any ideas? Thanks everybody.

Chris


Firstly there are two ways of creating a RegExp, and your example mixes
the two up.

The first is:-

r=new RegExp("Pattern", "Optional Switches g and i");

The second is a Regular Expression Literal

r=/Pattern/Optional Switches;

Note that with the first the Pattern must be in a string, and needs to
use escapes for backslashes \\

In terms of your requirement, I am sure others will have better
suggestions but you could try the following:-

var r=/^(\d{5}|\d{5}-\d{4}|[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d)$/;

Same as:-

var r=new RegExp("^(\\d{5}|\\d{5}-\\d{4}|[A-Za-z]\\d[A-Za-z]
\\d[A-Za-z]\\d)$");

Note that the ^ will prohibit any leading characters, and $ will
prohibit any trailing characters, the | is an alternative marker, and I
assume only one space between A#A #A#

Hope this helps.

Julian

Oct 20 '05 #2
Beautiful. Thanks for your help.

"Julian Turner" <ju****@baconbutty.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

Chris Kettenbach wrote:
Does anyone know of a regualr expression to validate US and Canadian
Postal
Codes?
I want to be able to accept ##### or #####-#### or Canadian A#A #A# that
is
alpha,number,alpha number,alpha,number.

I am new to regular expressions. The US 5 digit part is easy, but I get
stuck after that.

var pattern = new RegExp(/\\d{5}/)

Any ideas? Thanks everybody.

Chris


Firstly there are two ways of creating a RegExp, and your example mixes
the two up.

The first is:-

r=new RegExp("Pattern", "Optional Switches g and i");

The second is a Regular Expression Literal

r=/Pattern/Optional Switches;

Note that with the first the Pattern must be in a string, and needs to
use escapes for backslashes \\

In terms of your requirement, I am sure others will have better
suggestions but you could try the following:-

var r=/^(\d{5}|\d{5}-\d{4}|[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d)$/;

Same as:-

var r=new RegExp("^(\\d{5}|\\d{5}-\\d{4}|[A-Za-z]\\d[A-Za-z]
\\d[A-Za-z]\\d)$");

Note that the ^ will prohibit any leading characters, and $ will
prohibit any trailing characters, the | is an alternative marker, and I
assume only one space between A#A #A#

Hope this helps.

Julian

Oct 20 '05 #3
On 20/10/2005 16:35, Julian Turner wrote:

[snip]
var r=/^(\d{5}|\d{5}-\d{4}|[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d)$/;


Only slightly improved:

/^(\d{5}(-\d{4})?|[a-z]\d[a-z] \d[a-z]\d)$/i

The space prevents the Canadian postal code pattern from being reduced.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 20 '05 #4
Chris Kettenbach <c_**********@hotmail.com> wrote in message news:yO******************************@giganews.com ...
Does anyone know of a regualr expression to validate US and Canadian Postal
Codes?
I want to be able to accept ##### or #####-#### or Canadian A#A #A# that is
alpha,number,alpha number,alpha,number.

I am new to regular expressions. The US 5 digit part is easy, but I get
stuck after that.

Rather than using one catch-all expression, it may be better practice to
validate postcodes against the specified region.
--
S.C.

Oct 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Bradley Plett | last post: by
4 posts views Thread by Neri | last post: by
10 posts views Thread by Lee Kuhn | last post: by
3 posts views Thread by James D. Marshall | last post: by
7 posts views Thread by Billa | last post: by
9 posts views Thread by Pete Davis | last post: by
3 posts views Thread by Zach | last post: by
25 posts views Thread by Mike | last post: by
3 posts views Thread by Mr.Steskal | last post: by
18 posts views Thread by Lit | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.