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
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
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.
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
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconCOMRegularExpressions.asp
ASKER
does not help the link
ASKER
i try giving this on page_load
If Me.ddlCountry.SelectedItem .Text = "USA" Then
Me.regexpValidator_PostalC ode.Valida tionExpres sion = "\d{5}(-\d{4})?"
ElseIf Me.ddlCountry.SelectedItem .Text = "Canada" Then
Me.regexpValidator_PostalC ode.Valida tionExpres sion = "[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.
If Me.ddlCountry.SelectedItem
Me.regexpValidator_PostalC
ElseIf Me.ddlCountry.SelectedItem
Me.regexpValidator_PostalC
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
its not working... if i type the candian postal code still getting error
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"