Checking if participants' age is less than 18 | XM Community
Solved

Checking if participants' age is less than 18

  • 15 March 2022
  • 5 replies
  • 268 views

Badge +2

Hi,
I want to collect participants' date of birth in a form (together with other personal details), and then I want to check what their age is and if it is less than 18, I want to give them some kind of warning message so they notice it (much like HTML 5 does when it highlights the form). The purpose isn't to stop them from continuing, but instead to give them a gentle warning that they have possibly entered their DOB wrong. All the other similar questions were asked more than a few years ago when the custom code worked differently, so please don't point me to these posts as they are outdated to say the least! Thank you! :)

icon

Best answer by Aanurag_QC 16 March 2022, 13:23

View original

5 replies

Badge +30

Are you using a text box where people add numeric value or using a date picker? If you're using a date picker what date format are you using currently?

Badge +2

Thanks for you reply. I am using a text box (form field) with date validation (dd/mm/yyyy) and a placeholder.

Badge +30

I have created the basic logic here in JS

var from = "15/09/1994".split("/"); ---- Add the value which you receive from the question
var birthdateTimeStamp = new Date(from[2], from[1] - 1, from[0]);
var cur = new Date();
var diff = cur - birthdateTimeStamp;
// This is the difference in milliseconds
var currentAge = Math.floor(diff/31557600000);
// Divide by 1000*60*60*24*365.25

console.log(currentAge);
if (currentAge >=18){
  alert("Continue survey");
}
else{
  alert("discontinue");
}

You can use alerts that will pop up on their window which will notify the users on the go. If you want to discontinue them from using the survey, then you can store the value in an ED field and use branch flow logic saying if Age_Validator =Yes continue with other blocks else End the survey.

Badge +2

This is working wonderfully, but do you know (and I ask because this must be so easy for you) how to fetch the value from the form into the "from" variable?

Badge +30

You would need to place the JS in the question after the text entry question. You can use the pipe in value of the question which you can easily find either in the survey flow or in the survey questions and in the JS it would look something like this:

var a= "${q://QID1/ChoiceTextEntryValue}";

Leave a Reply