web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / How to calculate if us...
Power Pages
Unanswered

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

(0) ShareShare
ReportReport
Posted on by 299 Moderator

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:
I have the same question (0)
  • oliver.rodrigues Profile Picture
    9,368 Most Valuable Professional on at

    Hi, how far have you gone with your JS? 

    This would be the starting point to add JS validation: Add custom JavaScript to a form | Microsoft Learn

     

  • inanoffice23 Profile Picture
    299 Moderator on at

    I have gone all the way to customise 'validation', and worked with Arrays, hide/show fields, so I know a fair amount, it's just none of the codes I have tried for calculate age seems to work smoothly, and it seems the moment.js library is obsolete now

  • oliver.rodrigues Profile Picture
    9,368 Most Valuable Professional on at

    Can you check if this logic works?:

    var dob = $("#cr0ea_dob").val()
    
    var dobDate = new Date(dob);
    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");
    }
    
  • inanoffice23 Profile Picture
    299 Moderator on at

    It gives a DOB Date: Invalid Date, and also goes straight to the else statement.

     

    Should the date be a string, or a number? I tried parsing it into a number, but it won't even pick my input. I also tried adding the below and it doesn't work either

     

    $('#cr0ea_dob').change(function()
    {

     

    Am I correct in thinking that I should calculate it based on DD/MM/YYYY as leap years might affect the accuracy of going by year alone? 

  • oliver.rodrigues Profile Picture
    9,368 Most Valuable Professional on at

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

  • inanoffice23 Profile Picture
    299 Moderator on at

    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

  • inanoffice23 Profile Picture
    299 Moderator on at

    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

  • oliver.rodrigues Profile Picture
    9,368 Most Valuable Professional on at

    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
    299 Moderator on at

    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,368 Most Valuable Professional on at

    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

     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Pages

#1
Jerry-IN Profile Picture

Jerry-IN 71

#2
Fubar Profile Picture

Fubar 62 Super User 2025 Season 2

#3
sannavajjala87 Profile Picture

sannavajjala87 31

Last 30 days Overall leaderboard