Link to home
Start Free TrialLog in
Avatar of service2001
service2001

asked on

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
Avatar of vladimir_kalashnikov
vladimir_kalashnikov

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"
Avatar of service2001

ASKER

i guess there is validation control in asp.net called regular expression can't i use that?
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.
does not help the link
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.
ASKER CERTIFIED SOLUTION
Avatar of nisarkhan
nisarkhan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
its not working... if i type the candian postal code still getting error