validation - usa and canada zipcode

hi,

i able to validate how to check the zip code for usa

but how should i check for canada?

if the user clicks on the usa then should validate the zip code whether there is only 5 digits number

and if the user select as Canada as a country then it should validate the postal code for ane example: V5L3T2.

appreciate your help

thanks
service2001Asked:
Who is Participating?
 
nisarkhanCommented:
try this:

Expression:  ^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$
 
Sample Matches:  
44240|||44240-5555|||T2P 3C7
Sample Non-Matches:  
44240ddd|||t44240-55|||t2p3c7
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.  
0
 
vladimir_kalashnikovCommented:
just check to see if:

1) is 6 characters long
2) if the 1,3,5 characters are letters and if the 2,4,6 characters are numbers

you can check to see if a character is a letter by:

string postalCode = "V5L3T2";

if(postalCode[0] >= "a" && postalCode[0] <= "z")

you can do that for every character.  Note that for numbers if would be >= "0" and <= "9"
0
 
service2001Author Commented:
i guess there is validation control in asp.net called regular expression can't i use that?
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
vladimir_kalashnikovCommented:
yeah, you can use that.  It just requires you to say what the expression format should be.  That would be a good idea to use if this is for form validation.
0
 
vladimir_kalashnikovCommented:
here is a link if you want to use a regular expression and don't know how:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconCOMRegularExpressions.asp
0
 
service2001Author Commented:
does not help the link
0
 
service2001Author Commented:
i try giving this on page_load


        If Me.ddlCountry.SelectedItem.Text = "USA" Then
            Me.regexpValidator_PostalCode.ValidationExpression = "\d{5}(-\d{4})?"
        ElseIf Me.ddlCountry.SelectedItem.Text = "Canada" Then
            Me.regexpValidator_PostalCode.ValidationExpression = "[A-Z]\d[A-Z]\s\d[A-Z]\d"
        End If

but it does not work

how should i use, based on the user selection usa or canada the validaitonexpression should be assign and only check the validation of which country the user selected.
0
 
nisarkhanCommented:
Expression:  ^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$
 
 
Sample Matches:  
44240|||44240-5555|||G3H 6A3
Sample Non-Matches:  
Ohio|||abc|||g3h6a3
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.
0
 
service2001Author Commented:
its not working... if i type the candian postal code still getting error
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.