Community Groups are officially here!
We've released Groups, a new feature that enables us to connect community members of similar industries and interests in a shared, private space. You can check out all of the details here, including information about who can join, how to join, and what Groups are currently offered. Please leave your feedback through this Community Groups Feedback Survey.

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

2»

Answers

  • annaj
    annaj Qubie ✭

    @TomG I've aded in a previous comments the code I've added in (1).

    The issue is that when in (3) I choose:
    (a) Branch if eligible is Not Equal to 1 then End of Survey = I'm out of the survey no matter my DOB (older or younger than 15)
    (b) Branch if eligible is Equal to 1 then End of Survey = I'm not screened out of the survey, no matter my DOB (older or younger than 15)

    So it looks like the Embedded data attributed is always "1", which I don't understand.

  • Naids
    Naids South Africa Qubie ✭

    Hi, is there anyway this question can be done by Qualtrics? I am not familiar with JS. I tried to import the question from the library as in the first comment but then content validation is not available. Anyone know the easy way just to ask year born and qualifies age from there?

  • nkumich
    nkumich Ann Arbor, MI Qubie ✭

    Thanks for simplifying the solution @TomG. I've read your's and others' threads on moment.js and I think I have it set up right, but I must be missing something. Do I need to assign an embedded data field labeled "age" (I don't know how to do this.

    Here is what I've done so far, in the header:

    And this in the question JS field:

    Qualtrics.SurveyEngine.addOnload(function()
    {
    /Place your JavaScript here to run when the page loads/

    });
    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');
    });

    Qualtrics.SurveyEngine.addOnUnload(function()
    {
    /Place your JavaScript here to run when the page is unloaded/

    });

    Thanks for your expertise!

    @TomG said:
    I don't remember seeing this question before @mjs5az resurrected it, but the answers given are doing it the hard way and the accepted answer isn't always accurate. You can do everything @nkumich asked for by using moment.js and adding this script to the DOB question:

    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');
    });
    

    Demo survey

  • TomG
    TomG Raleigh, NC Wizard ✭✭✭✭✭

    @nkumich said:
    Thanks for simplifying the solution @TomG. I've read your's and others' threads on moment.js and I think I have it set up right, but I must be missing something. Do I need to assign an embedded data field labeled "age" (I don't know how to do this.

    You don't need to do anything additional with "age" for it to work. 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 it is not working, make sure you added the moment.js script to your survey header.

  • nkumich
    nkumich Ann Arbor, MI Qubie ✭

    Hmm, I checked the header, and it's good. It's still not validating. Should I be replacing any of the JS with my own code, e.g.** ("#"+this.questionId+" .InputText")**? Inserting the questionID?

  • TomG
    TomG Raleigh, NC Wizard ✭✭✭✭✭

    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).

  • Ashish_Kumar123
    Ashish_Kumar123 Noida, Uttar Pradesh, India Qubie ✭

    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
    TomG Raleigh, NC Wizard ✭✭✭✭✭

    @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:
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/moment.min.js"></script>
    
  • Ashish_Kumar123
    Ashish_Kumar123 Noida, Uttar Pradesh, India Qubie ✭

    Thanks @TomG ... It worked :)

  • Danny12077
    Danny12077 Qubie ✭

    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
    TomG Raleigh, NC Wizard ✭✭✭✭✭

    You 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.


  • TomG
    TomG Raleigh, NC Wizard ✭✭✭✭✭

    @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.

  • NikkiH
    NikkiH United States Qubie ✭

    @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
    TomG Raleigh, NC Wizard ✭✭✭✭✭

    My 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 <script> tag for you.