Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

5 digit Zipcode validation

Hi Team,

Kindly let me know the javascript or perl code to validate 5 digit zip code.


Thanks
Abdul
asked Oct 11, 2013 by Abdul Bronze (2,630 points)
I have prepared code to validate US zip code based on the below specified URL

http://www.city-data.com/zipDir.html

It works. However betweeen the zip code ranges there are some codes not available if any respondents entered those number as a zip code then my script won't work. And in the same time there are chances to enter zip code like this : 11111, 12345, 23456. These kind of entries should be flagged and terminate the respondent from survey. Anyone has come across this problem?

var count=0;
{

if (SSI_GetValue("Q13") < 01001 || SSI_GetValue("Q13") > 01890)
{
count++;
}
if (SSI_GetValue("Q13") < 01901 || SSI_GetValue("Q13") > 02816)
{
count++;
}
...
...
if (SSI_GetValue("Q13") < 99102 || SSI_GetValue("Q13") > 99733)
{
count++;
}
if (SSI_GetValue("Q13") < 99734 || SSI_GetValue("Q13") > 99929)
{
count++;
}

if count == 121
{
    
    alert("You have entered wrong Zipcode");
}

}

2 Answers

+3 votes
If the zipcode is strictly numeric, then I use a text box and sit it to a min and max of 5 characters.  Then I add this javascript in the validation area (where my question name is Contact_Zipcode):

var zip=SSI_GetValue("Contact_Zipcode");
if(isNaN(zip))
{
var strErrorMessage = "You have entered an invalid zipcode";
}

This way it will allow for a zipcode that starts with 0, which is problematic if you use a number field.
answered Oct 11, 2013 by Jay Rutherford Gold (36,195 points)
Does this JavaScript assume you have included a list of zip codes as a reference for validation that you call with the variable zip?
No, it just checks to make sure it's only a numeric response since it is a text field.  To check against an existing list you would most likely need to set that up as an array and check it.
+2 votes
You can try this one as well. You need to create text box with min and max character 5. and then use below code. My question name is Q70.

<script>

var checkOK2 = "0123456789";
var check2=new Array();
var allValid2 = true;
var str2 = document.mainform.Q70.value;
for (i = 0;  i < str2.length;  i++)
{
    ch1 = str2.charAt(i);
    for (j = 0;  j < checkOK2.length;  j++)
        if (ch1 == checkOK2.charAt(j))
    break;
    if (j == checkOK2.length)
        {
            allValid2 = false;
                break;
    }
}


if ((!allValid2) || str2.length!=5)
{
    strErrorMessage="Please enter a valid zipcode and click the Next button again.";
}

</script>
answered Oct 11, 2013 by Rajesh Rana Gold (24,770 points)
If I assume correctly your code will allow number '12345' or '11111' etc., right? As for as I am concerned about total number of zip codes (nearly 43k) which we cannot program. So we need to smart enough to program. By considering these, I have used this URL to atleast check number between the ranges. If any one answered outside of these ranges, then I will terminate them.
...