Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Pages - Customize & Extend
Answered

How to calculate if user is over/under 18 based on input field

(0) ShareShare
ReportReport
Posted on by 260 Super User 2025 Season 1

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

Categories:
  • oliver.rodrigues Profile Picture
    9,315 Most Valuable Professional on at
    Re: How to calculate if user is over/under 18 based on input field

    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

  • Verified answer
    inanoffice23 Profile Picture
    260 Super User 2025 Season 1 on at
    Re: How to calculate if user is over/under 18 based on input field

    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));
    }

     

     

  • oliver.rodrigues Profile Picture
    9,315 Most Valuable Professional on at
    Re: How to calculate if user is over/under 18 based on input field

    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 

  • inanoffice23 Profile Picture
    260 Super User 2025 Season 1 on at
    Re: How to calculate if user is over/under 18 based on input field

    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 &colon;)

    function onDisplaySectionChange() {
  • oliver.rodrigues Profile Picture
    9,315 Most Valuable Professional on at
    Re: How to calculate if user is over/under 18 based on input field

    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

     

  • inanoffice23 Profile Picture
    260 Super User 2025 Season 1 on at
    Re: How to calculate if user is over/under 18 based on input field

    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");
    }
    });

     

  • oliver.rodrigues Profile Picture
    9,315 Most Valuable Professional on at
    Re: How to calculate if user is over/under 18 based on input field

    OliverRodrigues_1-1705070721634.png

    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

  • inanoffice23 Profile Picture
    260 Super User 2025 Season 1 on at
    Re: How to calculate if user is over/under 18 based on input field

    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

  • inanoffice23 Profile Picture
    260 Super User 2025 Season 1 on at
    Re: How to calculate if user is over/under 18 based on input field

    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

  • oliver.rodrigues Profile Picture
    9,315 Most Valuable Professional on at
    Re: How to calculate if user is over/under 18 based on input field

    my date of birth field is a Date Only field in Dataverse

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics