Javascript to calculate user's age, based on DOB, check age eligibility, & provide validation alert | XM Community
Skip to main content
Solved

Javascript to calculate user's age, based on DOB, check age eligibility, & provide validation alert


Show first post

37 replies

TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5929 replies
  • November 29, 2018
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


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5929 replies
  • June 29, 2020

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.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5929 replies
  • July 30, 2020

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


  • 1 reply
  • October 12, 2020

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


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5929 replies
  • October 12, 2020

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.


  • 1 reply
  • April 23, 2021

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.


TomG
Level 8 ●●●●●●●●
Forum|alt.badge.img+27
  • Level 8 ●●●●●●●●
  • 5929 replies
  • April 23, 2021

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


Forum|alt.badge.img
  • 9 replies
  • February 8, 2022

I have a similar situation as to what's shown in this thread, but (I think) the main difference is how were are gathering DOB. We are not using a calendar, but rather a matrix table that captures month, day and year fields separately.
image.pngThe existing Javascript for the matrix table is attached here.
DOB script.txt
I would like to have a similar formula that can calculate the age. Is this possible with the matrix table too? If so, what would that process look like?


rolandrew
Level 2 ●●
Forum|alt.badge.img+2
  • Level 2 ●●
  • 6 replies
  • July 12, 2023

Hi @MohammedAli_Rajapkar ,

 

How exactly do I add an embedded data?

Is my code below correct?
 

Qualtrics.SurveyEngine.addOnReady(function()
{
var current_date = "${date://CurrentDate/DS}";
    var year1 = current_date.substr(0, 4);
    var Qyear = "${q://QIDSQ_yearofbirth/ChoiceTextEntryValue}";
    var captureAge = parseInt(year1) - parseInt(Qyear);
    Qualtrics.SurveyEngine.setEmbeddedData('UserAge', captureAge);

});

This is a screenshot of my question in Qualtrics: