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
> @mjs5zx said:
> @TomG This works. Thanks so much. Also, I really like how your demo survey automatically adds the '/' to assist with the date format. Would you be willing to help me learn how to do that?

@mjs5zx - I'll send you a message.
@TomG This works. Thanks so much. Also, I really like how your demo survey automatically adds the '/' to assist with the date format. Would you be willing to help me learn how to do that?
Userlevel 7
Badge +27
@mjs5zx - You can do age validation through some JavaScript trickery, but I don't think validation is the best choice. I think best practice is to terminate respondents that don't qualify. Otherwise, you are giving them to opportunity (i.e. encouraging them) to lie in order to move forward.

For the birthdate you already have, you can assign it to an embedded variable and pipe that embedded variable into your question as the default answer.
@TomG Basically, I want it to treat it like the baked in content validation and not allow the participant to proceed with the survey if the age is > 18, and to give them an error message. The birthdate is the first question. We've already collected it in a database application (Filemaker) that is passing some information to Qualtrics. It's not in the process currently, but it would probably be a good idea for me to pass the birthdate from Filemaker and create a flag if it doesn't match the birthdate entered into Qualtrics.
Userlevel 7
Badge +27
> @mjs5zx said:
> @TomG I want to create a validation message if the age is above > 18. Is there a way to do this without them submitting their response?

@mjs5zx - Yes there is, but your exact requirements aren't clear to me (since the original post wasn't yours). Whatever they are, the solution will be more complicated than what I posted.
@TomG I want to create a validation message if the age is above > 18. Is there a way to do this without them submitting their response?
I got it to work, I didn't need to change anything. My apologies.
@TomG I'm sorry, I'm not familiar with JS. So, I put this in the question html: ! but I'm not sure how to edit the JS that you provided. Where do I put the QID? Is there anything else that I need to change? I set an "Age" variable in the survey flow. Thanks so much for your help.
Userlevel 7
Badge +27
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
Using Mohammedali_Rajapakar_Ugam's method, I can't get the value from the text entry field for QYear to pipe in. I've tried using a form field to ask for the date in YYYY/MM/DD and MM/DD/YYYY formats.
Userlevel 1
Badge
To calculate age based on current DOB you can use the following code:

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 ageValue = 0;
var condition1 = parseInt(month+day);

var today_date = new Date();
var today_year = (today_date.getFullYear()).toString();
var today_day = (today_date.getDate()).toString();
var today_month= (today_date.getMonth()+1).toString();
var condition2 = parseInt(today_month+today_day);

if(condition2>=condition1)
{
ageValue = parseInt(today_year-parseInt(year));
}
else
{
ageValue = parseInt(today_year-parseInt(year)-1);
}

if(14<=ageValue && ageValue<=24)
{ Qualtrics.SurveyEngine.setEmbeddedData('Validity',1);
alert('Valid age.');
}
else
{ Qualtrics.SurveyEngine.setEmbeddedData('Validity',0);
alert('Invalid age.');
}

You'll need to add an embedded data 'Validity' in your Survey Flow and then use the display logic or survey termination condition on embedded data using the set embedded data.
Userlevel 7
Badge +20
The below code is to calculate the age based on the birth year:

Add an embedded data "UserAge" and the below code will assign the age of the user in it. Then in Survey flow, you can add branch logic to categorize user as eligible or ineligible.

!

Leave a Reply