I can't seem to find an accurate code to determine if user is over/under 18, has anyone managed to do that in Power Pages with Javascript? Thank you
I can't seem to find an accurate code to determine if user is over/under 18, has anyone managed to do that in Power Pages with Javascript? Thank you
that's great that you got it working, can you mark the best answer as the solution? this will help other users facing similar issue
Thank you Oliver, after many attempts, I managed to get this code working
function onDisplaySectionChange() {
var dob = $("#FIELDID").val();
function getAge(dateString) {
var today = new Date();
var dob = new Date(dateString);
var age = today.getFullYear() - dob.getFullYear();
var m = today.getMonth() - dob.getMonth();
if (m < 0 || (m === 0 && today.getDate() < dob.getDate())) {
age--;
}
return age;
}
console.log('age: ' + getAge(dob));
}
I see.. so if you want to trigger your code on the onchange you can follow this article: Date fields in Portals - deep dive - Dancing with CRM
if you want to add a validation to the form you can follow the documentation link I sent previously
once you get it working, make sure to mark the answer as being the solution in case other users have similar issue
I guess that is where the issue is, as it is in the Document ready section, I have many dropdowns that I can track, I was under the impression for non dropdowns we can use the below, but it seems I am missing something in my code. And yes I am adding it through the basic form - additional settings - javascript :)
function onDisplaySectionChange() {
The code seems correct.. but I have a question, how are you executing that code?
does it work when you do via Browser Console? or on the change of the DOB field?
if that's on the document.ready, it will only execute once, when the DOB field is empty, it won't refresh the value
you need to add a Validation and the logic would be part of the evaluation function: Add custom JavaScript to a form | Microsoft Learn
Thank you for your help Oliver, I am none the wiser, I will leave the code I am using below in case someone can spot anything
Date of Birth: (empty)
Type of DOB: string
Invalid Date
Minimum Date: Thu Jan 12 2006 15:07:40 GMT+0000 (Greenwich Mean Time)
DOB Date: Invalid Date
way too young friend
When I try to add a D.O.B. the code won't update either
$(document).ready(function() {
var dob = $("#gpbt_accountholderdob").val();
console.log("Date of Birth: " + dob);
console.log("Type of DOB: " + typeof(dob));
var dobDate = new Date(dob);
console.log(dobDate);
var minimumDate = new Date();
minimumDate.setFullYear(minimumDate.getFullYear() - 18);
console.log("Minimum Date: " + minimumDate);
console.log("DOB Date: " + dobDate);
if (minimumDate > dobDate) {
console.log("you are older than 18 years of age");
} else {
console.log("way too young friend");
}
});
using the typeof I also get string, but still the code works fine for me
there might be an issue somewhere in my code that is not counting timezone, so as you can see it's showing DOB as being 1 day before than it actually should be.. but this would only be a 1 day issue.. overall the logic should work
By the way I found out what type is giving me by adding a console.log(typeof dob); and it says it's a string
Yes, mine is also 'date only' in the dataverse, your code seems to calculate at the beginning, although with an invalid date, and if I add anything, it won't pick it up, I have tried many different codes, to no avail
my date of birth field is a Date Only field in Dataverse