I have been trying to get javascript to work for this but no luck. What I am needing is: I have a drop down box for a person to select COUNTRY either US or Canadian. If it is US then I need a zip code box to display and validate zip for US. If they select Canada I need the same box to validate for Canadian zip code. Any ideas or help with this would be very much appreciated. Can this be done in .asp? If not I can send the javascript code that I can't get to work if that would help.

<select name="COUNTRY" id="COUNTRY">
                <option value="United States">United States</option>
                <option value="Canada">Canada</option>
              </select>

<input name="ZIP" type="text" id="ZIP" size="7" maxlength="10" />

Dani AI

Generated

Server-side validation in Classic ASP is the right place for authoritative postal-code checks (client-side scripts are fine for UX but can be bypassed). Keep a single ZIP/postal text field and validate it on submit by reading the posted COUNTRY value and the ZIP value, normalizing the ZIP, then testing it with a regex that matches the expected format for that country. Populating a select with every US/Canadian postal code (as suggested by ) is impractical and poor UX for large datasets.

Use simple, well-known patterns: US ZIPs typically match ^\d{5}(-\d{4})?$ (5 digits, optional hyphen+4), Canadian postal codes match ^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$ (allowing an optional space or hyphen). Normalize input first (trim, uppercase for Canada) so stored values are consistent.

A compact Classic ASP (VBScript) validation function:

<%
Function IsValidPostal(country, zip)
    Dim re
    Set re = CreateObject("VBScript.RegExp")
    re.IgnoreCase = True
    zip = Trim(zip)
    Select Case LCase(Trim(country))
        Case "canada"
            re.Pattern = "^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$"
        Case "united states", "usa", "us"
            re.Pattern = "^\d{5}(-\d{4})?$"
        Case Else
            IsValidPostal = False
            Set re = Nothing
            Exit Function
    End Select
    IsValidPostal = re.Test(zip)
    Set re = Nothing
End Function

' Example: If Not IsValidPostal(Request.Form("COUNTRY"), Request.Form("ZIP")) Then ...
%>

Other practical notes: return friendly, encoded error messages and re-populate form fields when redisplaying the form; store a canonical format (e.g., uppercase no-space for Canada or keep the space consistently); consider HTML5 pattern or datalist for nicer input hints, or an AJAX/address-lookup service for authoritative validation when address accuracy is required. Always treat client input as untrusted and enforce the same checks on the server.

Recommended Answers

All 4 Replies

instead of textbox make the zip field a combobox and it's values will depend on what country you select.. you can do this thru javascipt..in this case you don't need to validate zip info...

So what you are saying is build a selection list that contains all the US zip codes and then one with all Canadian zip codes?

no.. just one selection list for all zip codes,, here try this code..

<html>


<head><title>Fill Zipcode</title></head>


<body>
<script type="text/javascript">
function fillzipcode(country)


{
//clear first before populating combobox
document.getElementById("zipcode").options.length = 0;


var combo = document.getElementById("zipcode");


if (country == "Canada")
{
//canada zipcodes here
var option = document.createElement("option");
option.text = "123"; option.value = "123";combo.add(option)



var option = document.createElement("option");
option.text = "321"; option.value = "321"; combo.add(option)



}
if (country == "USA")
{
//usa zipcodes here
var option = document.createElement("option");
option.text = "abc"; option.value = "abc";combo.add(option)



var option = document.createElement("option");
option.text = "cba"; option.value = "cba"; combo.add(option)


}
}
</script>
<form name="frm" id="frm">



Country :
<select name="country" id="country" onChange="fillzipcode(document.frm.country.value);">
<option value="">Choose One</option>
<option value="Canada">Canada</option>
<option value="USA">USA</option>
</select>
<br><br>
Zipcode :
<select name="zipcode" id="zipcode">
</select>



</form>
</body>
</html>

Thank you so much I will see what I can do with this code. I will have to create a selection list with all the zip codes which will take quite some time to do and find a list for Canadian zips to put with the US.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.