function calculateAgeLatest (birthday, dateToCalculate) { var calculateYear = dateToCalculate.getFullYear(); var calculateMonth = dateToCalculate.getMonth(); var calculateDay = dateToCalculate.getDate(); var dateOfBirth = new Date(birthday); var birthYear = dateOfBirth.getFullYear(); var birthMonth = dateOfBirth.getMonth(); var birthDay = dateOfBirth.getDate(); var age = calculateYear - birthYear; var ageMonth = calculateMonth - birthMonth; var ageDay = calculateDay - birthDay; if (ageMonth < 0 || (ageMonth == 0 && ageDay < 0)) { age = parseInt(age) - 1; } return age; } function addAgeValidator(fieldName) { if (typeof (Page_Validators) == 'undefined') return; // Create new validator $("#" + fieldName + "_label").parent().addClass("required"); var newValidator = document.createElement('span'); newValidator.style.display = "none"; newValidator.id = "RequiredFieldValidator" + fieldName; newValidator.controltovalidate = "casetypecode"; newValidator.errormessage = "Date of birth can't be in the future"; newValidator.validationGroup = ""; newValidator.initialvalue = ""; newValidator.evaluationfunction = function () { //var value = $("#" + fieldName).val(); var birthdate = $("#" + fieldName).val(); var todaysdate = new Date(); var dateOfBirth = new Date(birthdate); if (dateOfBirth > todaysdate) { return false; } else { return true; } //if (calculateAgeLatest(birthdate,new Date()) < 1) { // return false; //} else { // return true; //} }; // Add the new validator to the page validators array: Page_Validators.push(newValidator); // Wire-up the click event handler of the validation summary link $("a[href='#" + fieldName + "_label']").on("click", function () { scrollToAndFocus(fieldName + '_label', fieldName); }); } function addValidator(fieldName, fieldLabel) { if (typeof (Page_Validators) == 'undefined') return; // Create new validator $("#" + fieldName + "_label").parent().addClass("required"); var newValidator = document.createElement('span'); newValidator.style.display = "none"; newValidator.id = "RequiredFieldValidator" + fieldName; newValidator.controltovalidate = "casetypecode"; newValidator.errormessage = "" + fieldLabel + " is a mandatory field."; newValidator.validationGroup = ""; newValidator.initialvalue = ""; newValidator.evaluationfunction = function () { var value = $("#" + fieldName).val(); if (value == null || value == "") { return false; } else { return true; } }; // Add the new validator to the page validators array: Page_Validators.push(newValidator); // Wire-up the click event handler of the validation summary link $("a[href='#" + fieldName + "_label']").on("click", function () { scrollToAndFocus(fieldName + '_label', fieldName); }); } function removeValidator(fieldName) { $.each(Page_Validators, function (index, validator) { if (validator.id == "RequiredFieldValidator" + fieldName) { Page_Validators.splice(index, 1); } }); $("#" + fieldName + "_label").parent().removeClass("required"); } //add validator to make sure date of birth is a realistic addAgeValidator('citb_dob'); var birthdate = $('#citb_dob').val(); if (birthdate && calculateAgeLatest(birthdate,new Date()) < 18) { $("label[for='citb_parentguardingname']").parent().attr('class', 'info required'); addValidator('citb_parentguardingname', 'Parent Guardian Name'); $("label[for='citb_relationshiptocarer']").parent().attr('class', 'info required'); addValidator('citb_relationshiptocarer', 'Relationship to Carer'); } else { $('#citb_parentguardingname').css('display', 'none'); $("label[for='citb_parentguardingname']").css('display', 'none'); $('#citb_relationshiptocarer').css('display', 'none'); $("label[for='citb_relationshiptocarer']").css('display', 'none'); } // this is triggered once date of birth is changed var dpcontrol = $('div.control')[5]; $(dpcontrol).on("dp.change", function (e) { var birthdate = e.date; var age = calculateAgeLatest(birthdate,new Date()); if (age < 18) { $("label[for='citb_parentguardingname']").parent().attr('class', 'info required'); addValidator('citb_parentguardingname', 'Parent Guardian Name'); $("label[for='citb_relationshiptocarer']").parent().attr('class', 'info required'); addValidator('citb_relationshiptocarer', 'Relationship to Carer'); $('#citb_parentguardingname').css('display', ''); $("label[for='citb_parentguardingname']").css('display', ''); $('#citb_relationshiptocarer').css('display', ''); $("label[for='citb_relationshiptocarer']").css('display', ''); } else { removeValidator('citb_relationshiptocarer'); $('#citb_parentguardingname').css('display', 'none'); $("label[for='citb_parentguardingname']").css('display', 'none'); removeValidator('citb_parentguardingname'); $('#citb_relationshiptocarer').css('display', 'none'); $("label[for='citb_relationshiptocarer']").css('display', 'none'); } });