Javascript to calculate user's age, based on DOB, check age eligibility, & provide validation alert | XM Community
Skip to main content
A major goal of our recruitment survey is to identify users who are between the ages of 14-24 (target population) and those who are not (not eligible for our study). Currently we ask users for their date of birth (MM/DD/YYYY) and would like to use JS to:



1. Calculate age, based on the current date

2. Determine if the age is within our target range (14-24)

3. Provide a validation alert to the users AND/OR categorize users using embedded data in the categories of eligible/ineligible based on age



Please take a look at the code I have now for the DOB question:



Qualtrics.SurveyEngine.addOnReady(function()

{

var dob_entry = getTextValue();

var split_dob = dob_entry.split("/");

var month = split_dob[0];

var day = split_dob[1];

var year = split_dob[2];

var dob_asdate = new Date(year, month, day);

var today = new Date();

var mili_dif = Math.abs(today.getTime() - dob_asdate.getTime());

var age = (mili_dif / (1000 * 3600 * 24 * 365.25));

within_age_range=(14<age & age<24);



alert(age);

});
No. "this" is the current question object and "this.questionId" is the current question id (e.g. QID1").



Are you using single line text entry question?

Are you entering the date in a format moment can parse?



It might help if you post a preview link to your survey (or a block that contains your DOB question).

Hi TomG,
Could you please let me know how do I load moment.js in survey header. Because when I have load whole JS codes "Qualtrics.SurveyEngine.addOnPageSubmit(function() {  var age = moment().diff(moment(jQuery("#"+this.questionId+" .InputText").val()), 'years');  Qualtrics.SurveyEngine.setEmbeddedData('age', age);  if(age > 13 && age < 25) Qualtrics.SurveyEngine.setEmbeddedData('eligible', '1');});" in survey header. Then the whole code is appearing on the screen. And code is not running.

Please let me know if I have to load any specific code?
Regards,
Ashish


Ashish_Kumar123 ,

  1. You need to be in html source mode when you add JS to the header. Click on the
    <>
    icon.

  2. The addOnPageSubmit function should be attached to the question, not in the header.

  3. Put this in the header:



Thanks TomG ... It worked :)


Hi TomG

Great solution! I'm British, and as such need the above solution to work for a dd-mm-yyy format. Do you know how I might go about achieving this?
I assumed moment would detect that the given date was in that format, but it does not.


https://www.qualtrics.com/community/discussion/comment/28397#Comment_28397You can specify the format when you parse the date with moment.


Hi TomG ,
Thanks for all of your help on this topic. I tried using your code quoted below, and after submitting a survey response, the output CSV file is leaving the data in the "age" column blank. I'm not very familiar with using Javascript or with using code within Qualtrics in general. Do I need to add an embedded data element in the survey flow with the variable "age" as well (I assume leaving its value blank in the survey flow), or does the Javascript code within the DoB question do the job on its own? I tried adding the embedded data element in the survey flow both above and below the question block, but it still didn't work for me.
https://www.qualtrics.com/community/discussion/comment/6695#Comment_6695


tun44151 ,
Yes, as I said in a post above, "However, if you want age saved in your response data, you need to initialize it in your survey flow before the block with the DOB question."
If you've done that and it isn't working, you've made a mistake someplace else.


TomG - hello there! I've been trying to put the "calculate age from date of birth and determine eligibility code" into my survey but struggling with it. I am new to Qualtrics and definitely have no JS background. I understand what your JS code (above) is trying to do and have educated myself on Embedded Data, Survey Flows. I've read all the linked threads and previous answers in this thread but it doesn't seem to be working. Unfortunately Qualtrics Tech Support couldn't help me with the JS but they were saying it wasn't calculating the age. I don't have the knowledge to understand why it is not working - I have been trying to test it in a one-question survey to no avail.
Any help would be appreciated; I can share the one-question test survey if that is helpful.


https://www.qualtrics.com/community/discussion/comment/36777#Comment_36777My guess would be that the jsdelivr src in the header is wrong. In the message above the Qualtrics Community automatically replaced a string that contained an @ with [email_protected]. If that is the problem, go to jsdelivr search for moment.js. jsdeliver will create the correct

Scanning file for viruses.

Sorry, we're still checking this file's contents to make sure it's safe to download. Please try again in a few minutes.

OK

This file cannot be downloaded

Sorry, our virus scanner detected that this file isn't safe to download.

OK