Calculating Age in Simple layout with Side-by-Side question | XM Community
Skip to main content

I need to create an age verification question using the side-by-side matrix format from the Qualtrics shared library. The end goal is to collect date of birth, verify if the respondent is 18 or older, then allow them to complete the survey (if yes). We use the Simple Layout in this survey.

I tried using embedded data in the survey flow to calculate age and can get close, but it’s not accurate enough for our needs (ages are off by 1-2 weeks due to leap years, etc.). 

 

 

I assume a custom code solution is the next best option but I’ve had no luck transferring the concepts from this code (below) to a side-by-side matrix in Simple Layout. 

 

https://gist.github.com/marketinview/543413f36b34ba8bc71d97d4fd489b9d

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var DateTime = luxon.DateTime;
var birthDate = DateTime.fromFormat(jQuery("#"+this.questionId+" .InputText").val(),"MM/dd/yyyy");
var age = DateTime.now().diff(birthDate, f'years','months']).toObject();
Qualtrics.SurveyEngine.setJSEmbeddedData('age',age.years);
});

 

Has anyone successfully implemented something like this?  Appreciate any help the group can provide!

Hi @Adam_Jermain ,

This will require additional update to make it compatible with Side by Side question.
If you can share the side by side structure , then I may be able to provide you with the code.


To use above question as such , please find below approach :

 

Create text entry Question  as above and add custom validation to it.

Additionally , add the below html code to the questions as below   :
 


 

Please Enter you age in mm/dd/yyy format<br><script src="https://cdn.jsdelivr.net/npm/luxon@3.4/build/global/luxon.min.js"></script>



Then add the previously shared code in the question JS API:

 


Now for a last step declare the below Embedded variable at the top of the survey :

__js_age

Now the age will be calculated automatically using  JS and will be stored in above variable which can be used for validation.

Hope this resolves your query😊!!


Thanks @qualtrics_nerd! The side-by-side format is basic:

 

(I built a similar text-based date input successfully using the technique you described, but my stakeholder insists on the dropdown menu format instead.)


Leave a Reply