This script allows you to ask for a participant's year of birth in a Number question. Using the provided year of birth value, the script jumps to the relevant calendar year in a Date question, and populates the age value in another Number question.
Note:
- The age value is static and captured at the time the script executes. It will not change over time.
- The date of birth is calculated based on January 1 of the given year.
Detailed Steps
- In your survey, create a new Page element.
- Add a Number question to the Page element.
The script works with either Numeric Text Field or Numeric Slider questions. Select a display option and perform the additional configuration steps.
Display answers as Additional configuration Text field - Set Minimum and maximum response length to 4 and 4, respectively.
Slider - Set Minimum and maximum values to the lowest and highest birth years, respectively (for example, 1920 and 2020).
- Ensure Use decimals is set to No.
- Ensure Display values is set to No.
- Add a second Page element.
- Add a Date question and a Number question to the second Page element.
You may want to temporarily leave these unhidden for testing. - Open the first Page element for editing and click the Scripting button.
- Copy and paste the script below into the On Complete tab.
-
Replace YearOfBirth with the first Number question's name.
This has to be done 3 times in the script. - Replace AgeCalculated with the second Number question's name.
- Replace DateOfBirth with the Date question name.
- Click Save.
- Preview and test your survey.
Script
/* How do I use the birth year response from a Number question
to set the calendar in a Date question and calculate age? (Modern Surveys)
To ensure your script works as expected, always copy the original script
from the Script Library found in the Alida Help Center. Failure to use the
latest script from the script library may cause unexpected results. */ const year = response.getDataPoint("YearOfBirth");
const combined = response.getDataPoint("DateOfBirth");
const ageCalc = response.getDataPoint("AgeCalculated");
// Get the entered data
var y = year.get();
var regexpcode = /^\d{4}$/;
//If the inputString is NOT a match
if (!regexpcode.test(y) || y == null) {
response.setError("YearOfBirth", "Please enter a valid year of birth");
}
var current_year=new Date().getFullYear();
if((y < 1920) || (y > current_year)) {
response.setError("YearOfBirth", "Year should be in range 1920 to current year");
}
// New date object on the 1st of the given year and month (month is zero based)
var date = new Date(y, 0, 1);
// Calculate age
var diff_ms = Date.now() - date.getTime();
var age_dt = new Date(diff_ms);
var aget = age_dt.getFullYear() - 1970;
//Set date and DOB from January 1st and entered year
ageCalc.set(aget);
combined.set(date);
Example
Sample survey structure:
First page with Number question asking for year of birth:
Second page with Date question showing the calendar year for participant's year of birth, and Number question showing the auto-calculated age:
Comments
0 comments
Article is closed for comments.