Online Survey Tutorials

SurveyGizmo Tutorials and Help Documentation

Tutorial: Scripting Sample: Age Verification by Birth Date

This series is dedicated to showing advanced functionality within SurveyGizmo using the Custom Scripting action (Pro plan or higher). While the tutorials are meant to be portable and easy to insert into your own survey, it will be necessary to understand advanced concepts such as Question IDs. These scripts are provided as-is without additional support for alterations.

This script uses the following question types or actions:

  • Text Field Question (optional – Data Format: Date (MM/DD/YYYY))

Description

This script will perform age verification based upon someone’s date of birth, submitted through an open text field. The qualifying age can be set in the script (18 years old, 21 years old, etc.) and if the user is not old enough, they are disqualified. If they are old enough, the script does nothing. The disqualification message is also set in the script.
There is an optional section that can be turned on that will also set a maximum age for the respondent.

The Script

// Text field's Question ID
%%textboxqid = 3;
 
// Age Limit - below this age (in years) will be disqualified
%%agelimit = 21;
 
/** OPTIONAL SECTION **/
// uses the agelimit as the minimum
%%agerange = 0; // Change to 1 to use this optional feature
%%agemax = 55;
 
// Disqualification Message
%%msg = "I'm sorry, you are not old enough to take this survey. Thanks for your time!";
 
 
/**** DO NOT ALTER BELOW THIS POINT ****/
%%birthdate = sgapiGetValue(%%textboxqid);
%%birthdate_unix = sgapiStrtotime(%%birthdate);
 
%%today = sgapiDate("Y-m-d");
%%today_unix = sgapiStrtotime(%%today);
 
%%validdate_unix = sgapiStrtotime('-'.%%agelimit.'years',%%today_unix);
 
if (%%agerange == 1){
  %%maxdate_unix = sgapiStrtotime('-'.%%agemax.'years',%%today_unix);
  if (%%birthdate_unix > %%validdate_unix ||
      %%birthdate_unix < %%maxdate_unix)
  {
    sgapiDisqualify(%%msg);
  }  
 
}else{ // if agerange is 0 or anything besides 1
  if (%%birthdate_unix > %%validdate_unix)
  {
    sgapiDisqualify(%%msg);
  }
 
}

The Steps

  • On the initial page, Add Question > Text Field and if you like, set the Data Type to Date (Advanced Edit)
  • On the page following, Add Action > Custom Scripting Control and paste in the above code
  • Alter the script to include the correct question ID and age (in years)
  • Save all changes and test in preview mode
  • Optional: Change %%agerange = 1 and set your %%agemax to enable optional check

A sample survey to demonstrate the script above is available! Age Verification Sample Survey

Warnings

This script will understand the following formatting of the date field (as well as the ‘Data Type: Date’ advanced formatting option:

  • All of these formats represent the 1st of February, 2003
  • mm/dd/yyyy – 02/01/2003
  • mm/dd/yy – 02/01/03
  • yyyy/mm/dd – 2003/02/01
  • dd-mm-yyyy – 01-02-2003
  • yy-mm-dd – 03-02-01
  • yyyy-mm-dd – 2003-02-01