Scriptlet: Calculate Lead's Age

If "birthdate" is collected on your ion form, calculate a lead's age by running a scriptlet with advanced rules, behind the form. If leads need to meet an age requirement to be eligible for an offer, the scriptlet can return a true/false value to confirm if the age requirement is met. Based on the scriptlet result, advanced rules can be used to control the user experience.

// Calculates age based on birthdate. 
// Returns true if lead is 18 or older.
// Optionally save age into LiveBall data collection field.

function Age(monthDob,dayDob,yearDob) {
  var now = new Date();
  var yearNow = now.getFullYear();
  var monthNow = now.getMonth() + 1;
  var dayNow = now.getDate();

  yearAge = yearNow - yearDob;

  if (monthNow <= monthDob) {
    if (monthNow < monthDob) {
      yearAge--;        
    }
    else {
      if (dayNow < dayDob) {
        yearAge--;
      }
    }
  }
 
  return yearAge;
}

// form field birthday is formatted as mm/dd/yyyy
var date = respondent.reg_month + '/' + respondent.reg_day + '/' + respondent.reg_year;
var myDate = new Date(date);
var myAge = Age(myDate.getMonth() + 1, myDate.getDate(), myDate.getFullYear());
//actionSaveData("age",yearAge.toString());
if (myAge > 17) {
    actionSaveData("ageRequirement","true");
}
else {
    actionSaveData("ageRequirement","false");
}