Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regular expressions challenge!

Status
Not open for further replies.

tachyon80

Technical User
Joined
Jun 19, 2004
Messages
13
Location
US
I'm trying to build a regular expression for Canadian postal codes for javascript but I'm not really up to the task. Can anyone here do it?

I needs to be:
letter (upper case only)
number
letter (upper case only)
no space OR space OR dash
number
letter (upper case only)
number
And then no more.

What would this regular expression look like?
 
>>no space OR space OR dash


what does that mean, can i have a example???

Known is handfull, Unknown is worldfull
 
/[A-Z]\d[A-Z](\s?|-)\d[A-Z]\d$/

I guy wrote that for me and it seems to work.
(\s?|-) seems to be the "no space OR space OR dash.
 
Funny....I recently did a lot of searching on this.
Canadian Postal codes not only have the
alpha-number-alpha-number-alpha-number pattern
you also have to consider the following

Canadian Postal Codes

1. Six characters [alpha][number][alpha][space][number][alpha][number] . The space may or may not be present.
2. First Char identifies the geographical region. ABCEGHJKLMNPRSTVXY (only values allowed for first char)
3. Letters I and O are not allowed. (they get confused with numbers 1 and 0)

ie K1A 0B1

I ended up with the following expression.
Code:
/^[A-CEHJ-NPR-TVXY]\d[A-HJ-NP-Z](\s[b]|-[/b])?\d[A-HJ-NP-Z]\d$/

I'm not sure about the dash you mention. I believe the Canadian Postal Standards doesn't mention a dash. However I added the dash on the reg exp presented above. Up to you if you want to keep it or not


You can check the following for reference.


This is like programmers deja vu I worked on this just three days ago..... weird ....

grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
-----------------------------------------------
 
Thanks. Those are some good points to consider.
 
this:
/[A-Z]\d[A-Z](\s?|-)\d[A-Z]\d$/
is correct...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top