Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Any one please provide the regular expression for this validation


Canadian Postal Code can be shown as A1A 1A1 or A1A1A1 (where A can be any letter and 1 can be any number 0 to 9).
Posted

1 solution

Hi,

Try this RegEx:
^[A-Z]\d[A-Z][ ]?\d[A-Z]\d$

But a warning: a regular expression to validate a postcode it's not sufficient to guarantee that the postcode really exists!
Please see: http://stackoverflow.com/a/11149740[^]

A regex approach can validate the format of a Canadian postcode, but it's not sufficient to guarantee that the postcode actually exists.



For example: A9A 0A0 looks like a valid Canadian postcode, but the forward sortation area A9A doesn't actually exist.



Are you sure you wouldn't rather do some kind of lookup against an official list of postcodes?

 
Share this answer
 
v2
Comments
Sushil Mate 10-Jul-13 5:59am    
mine deleted, +5
Thomas Daniels 10-Jul-13 5:59am    
Thanks for your vote!
RaisKazi 10-Jul-13 9:52am    
Good Answer. :)
Thomas Daniels 10-Jul-13 9:54am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900