Calculate Age From Birthday

Important Update to Custom Scripting

SurveyGizmo's CustomScript Action now supports the LUA programming language. Visit our NEW Lua Scripting Resources!

Legacy Custom Scripting Language Deprecation Plans 

  1. New accounts (created after October 29, 2018) will only have the option to use Lua in scripts.
  2. As of October 29, 2018 Custom Scripting Actions will default to Lua as the scripting type in the Custom Scripting Action for accounts created before this date. You will be able to switch to the Legacy Custom Scripting; though we highly encourage using Lua.
  3. In the long term, Legacy Custom Scripting Actions will be switched to read-only. The exact date on this is to be determined; we will send notifications well ahead of time.

This JavaScript will perform age verification based on the date of birth entered in 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 will be disqualified. If they are old enough, the script does nothing and allows them to move forward through the survey. You can also customize the disqualification message as well. If you prefer Custom Scripting to JavaScript, check out our tutorial on Age Verification by Birth Date in Custom Scripting. 

Check it out in a survey:

http://www.surveygizmo.com/s3/2033346/Working-Example-Calculate-Age-from-Birth-Date

OR

Add a survey with this setup to your account!

This script uses the following question types or actions:

  • Textbox Question (for Birth Date)
  • Textbox Question (for Age) Or Hidden Value
  • Javascript Action

This workaround works for most of our customers in most cases but may require some tweaking to get it right. If you have a solution that works better let us know!

Setup

First build out your survey page with your birth date textbox and age textbox or hidden value. Next click Add New Action > JavaScript. Paste the below JavaScript. 


$(document).ready(function(){
if($('body').attr('id') != 'app') {

var textbox = $('#sgE-2033346-1-2-element');  //dob box
$tgtbox = $("#sgE-2033346-1-4-element"); //age box
$minage = 18;  //minimum age
$errortxt = 'You must be at least ' + $minage + ' years old to continue';
  
  
textbox.change(function(){
console.log($(this).val());
var birth = $(this).val();

function getAge(birth) {

var d = new Date();
var n = d.getTime();
console.log(n);

var c = Date.parse(birth);
console.log(c);

var a = (d - c) /(1000*60*60*24*365);
var result=Math.round(a*100)/100;
console.log(result);

   $tgtbox.val(Math.floor(result));
   //alert(result);
}
getAge(birth);
});
}

  
//submit validation  
$('.sg-survey-form').submit(function(e){
  
  if($tgtbox.val() < $minage) {
    //alert($tgtbox.val());
    //alert($minage);
    
    $('.sg-error-message').text($errortxt).fadeIn('slow');
    e.preventDefault();
    
  }
});  //end submit validation
});

Required Modifications

The highlighted blue elements above will need to be changed to match your survey. 

The element var textbox = $('#sgE-2033346-1-2-element'); will need to be updated with the birth date textbox element ID.

The element $tgtbox = $("#sgE-2033346-1-4-element"); will need to be updated with the element ID of the hidden value or textbox associated with the respondent's age. 

$minage = 18;  //Minimum Age-This is the age limit you would like to set. Any response received with an age limit below this age (in years) will be disqualified. For example, if you do not want anyone under the age of 30 to fill out your survey, you'd want to change the number 18 to 30. 

To learn how to find your individual element IDs check out our awesome tutorial on How to Find Element IDs

Scripting and Other Custom Solutions

We’re always happy to help you debug any documented script that is used as is. That said, we do not have the resources to write scripts on demand or to debug a customized script.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for SurveyGizmo features and functionality ideas that might meet your customization. Beyond this, you might want to consult with someone on our Programming Services Team; these folks might have the scripting chops to help you to achieve what you are looking for!