I currently have the following Javascript action on one of my SurveyGizmo surveys.
The purpose of it is to calculate the exact age (by a specific date) of when someone specifies their birth date.
However, we often prefill info into our surveys. The problem is that the script doesn't trigger when their is a date already prefilled into the dob textbox. Is there a way to ensure the age textbox updates whenever the page is loaded as well as when the textbox is changed?
Thank you for any help you may provide/
$SG(document).ready(function(){
if ($SG('body').attr('id') != 'app') {
var textbox = $SG('#sgE-2604126-33-6-element'); //dob box
$tgtbox = $SG("#sgE-2604126-33-254-element"); //age box
$minage = 21; //minimum age
$errortxt = 'You must be at least ' + $minage + ' years old to continue';
textbox.change(function(){
console.log($SG(this).val());
var birth = $SG(this).val();
function getAge(birth) {
var d = new Date(2016,3,16);
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=(a*100)/100;
console.log(result);
$tgtbox.val(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
});