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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validate zip code

Status
Not open for further replies.

exitsystem

Programmer
Aug 13, 2004
1
US
validate zip code
I am trying to validate canada zip code using the following function in asp:

[a-zA-Z]\d{1}[a-zA-Z]\b \d{1}[a-zA-Z]\d{1}
(it works in .NET)

For some reason space is not recognized.
I also tried using \x20 to represent space.
It is also not working.
Does anyone knows what is the issue and how to fix it?

Thanks much.

PS canadian zip example: Z5Z 5Z5
char(1) integer char(1) space integer char(1) integer
 
Actually it gets a little bit more complicated than that.
Canadian postal codes dont have the letters I,O in them.
The first Letter defines the geographic region and is made only of a subset of chars.
I use the following to validate ZIP codes and Postal Codes

Code:
var zippat = /^\d{5}$|^\d{5}[\-\s]?\d{4}$|^[a-cehj-npr-tvxy]\d[a-hj-np-z](\s)?\d[a-hj-np-z]\d$/i;
//Zip validation for US: 5 digits only, or 9 digits with a hyphen or space in between. 
//Postal Code validation for Canada: 6 chars (3 sets of [alpha][number] ). First Char identifies the Region.No I,O chars allowed

The /i at the end makes the expression case insensitive, that way I avoid the [a-zA-Z] headache.

Short answer to your question: use \s[\b] to represent a single space.

grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
-----------------------------------------------
 
can i have an example of a valid canadian zip code???
 
T5J 0J8
all the sequences seem to be this pattern:
/[A-Z]\d[A-Z]\s\d[A-Z]\d/



Known is handfull, Unknown is worldfull
 
vbkris,

re-read grtfercho's post:
Canadian postal codes dont have the letters I,O in them.
The first Letter defines the geographic region and is made only of a subset of chars.

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
he can modify it then to something like this?
/[A-H]|[J-N]|[P-Z]\d[A-H]|[J-N]|[P-Z]\s\d[A-H]|[J-N]|[P-Z]\d/



Known is handfull, Unknown is worldfull
 
var zippat = /^\d{5}$|^\d{5}[\-\s]?\d{4}$|^[a-cehj-npr-tvxy]\d[a-hj-np-z](\s)?\d[a-hj-np-z]\d$/i;
//Zip validation for US: 5 digits only, or 9 digits with a hyphen or space in between.
//Postal Code validation for Canada: 6 chars (3 sets of [alpha][number] ). First Char identifies the Region.No I,O chars allowed

Second post on the thread, Aug 13..


The second part of the reg expression validates Canadian Postal Codes, the first part is for american Zip codes.

:) :)


grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
-----------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top