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

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



Show first post

37 replies

Userlevel 7
Badge +27
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

Userlevel 7
Badge +27

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.

Userlevel 7
Badge +27

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

Userlevel 7
Badge +27

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.

Userlevel 7
Badge +27

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