How do I validate zipcode text entry against a list of approved zipcodes | XM Community
Solved

How do I validate zipcode text entry against a list of approved zipcodes

  • 18 October 2019
  • 6 replies
  • 560 views

I want to ask the question "What is your zipcode?" as a text entry question and then based off the entered zipcode either terminate the survey or continue to next question. I have a list of 3000+ zipcodes and the respondent must live in one of the zipcodes in order to continue the survey. I tried using the Authenticator, but that only allows for the zipcode to be entered one time and there might be many respondents that live in one zipcode. Any tips?
icon

Best answer by TomG 18 October 2019, 20:52

View original

6 replies

Userlevel 2
Badge +1
hi @HE_Survey - Within the Authenticator -> Options there is an option mentioning - "Allow authenticating respondents to retake authenticated section" you can select this and have multiple people from the same location take your survey with using the Authenticator, see image attached. Hope this helps.

!
Userlevel 7
Badge +27
@HE_Survey,

Sorry, but you don't want to do what @Igee suggests. Respondents with the same zip code would be overwriting each others responses.

An Authenticator is not the right solution. The best solution is to use a webservice call that queries an external database to tell you if the person's zip code is in the list of valid zip codes. The webservice would return a variable that tells you if a valid zip code was entered. Then use a branch condition to terminate those that aren't valid. Unfortunately, you need access to a web server and the ability to write a script to do this.

If you can't manage that, a secondary solution would be to put all 3000 zip codes in a JavaScript array, then search the array for a match and set an embedded variable based on whether a match was found.

You haven't said what the 3000 zip codes represent, but if they represent a state or group of states you could use the built-in QState_ExportTag_suffix embedded variable and branch based on that (the idea being that it could possibly it to a manageable number that could be used in branch logic). See built-in embedded data for more info.
Badge

I am facing the same problem. I need to validate the zip codes of a specific US state against a text entry question of “What is your zip code?”.

@TomG, I want to do the process of matching the text entry to a Java array. I have found a set of code but it’s not working. Do you know any correct source of code for this process?

Thanks

Userlevel 7
Badge +27

@RahmanM,

Use:

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var zipArray = [ /* list of zip codes here */];
if(zipArray.includes("#"+this.questionId+" .InputText")) { /* zip match, do something */ }
});

 

Badge

@TomG, thanks for sharing the code. I tried it. I put 5 zip codes as example in the array and did a text entry (which is not in that 5 zipcodes) but it’s not working. The survey can still be taken with the wrong zipcode. Am I missing something?

Thanks

Userlevel 7
Badge +27

@TomG, thanks for sharing the code. I tried it. I put 5 zip codes as example in the array and did a text entry (which is not in that 5 zipcodes) but it’s not working. The survey can still be taken with the wrong zipcode. Am I missing something?

Thanks

The code is meant as a starting place for you to add to as indicated by the comments, not to work as-is.  Post the code you are using. 

Leave a Reply