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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

I have to calculate age based on Date of Birth in Jscript and if the age less than 18 it has to throw an error and the value in the date of birth field to change as empty.

(0) ShareShare
ReportReport
Posted on by

Hi,

I have two fields new_dateofbirth and new_age.

data type of new_dateofbirth is "Date and Time" and data type of new_age is Whole Number.

If I give the value in new_dateofbirth field, age has to display in new_age field.

I have created a Jscript as given below and it shows the age correctly in new_age field but i want to add the script like if the age is less than 18 it has to throw an error and once we click ok in the error, value in the new_date_birth field should get clear.

function CalculateAge()
{
if(Xrm.Page.getAttribute("new_dateofbirth").getValue() != null)
{
       var now = new Date();
       var birthday = Xrm.Page.getAttribute("new_dateofbirth").getValue();
       var monthdif = now.getMonth() - birthday.getMonth();
       var nowyear = Xrm.Page.getAttribute("new_dateofbirth").getValue().getFullYear();
       if(monthdif > -1)
       {
                  
                Xrm.Page.getAttribute("new_age").setValue(now.getFullYear() - birthday.getFullYear());
       }
       else
       {
               Xrm.Page.getAttribute("new_age").setValue(now.getFullYear() - birthday.getFullYear()-1);
       }
}
}

Thanks in advance for the answers.

Regards,

Vignesh Babu

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Sartaj Profile Picture
    880 on at

    Hi Vignesh,

    I done changes in your code to fit your requirement , give it a try.

     function CalculateAge()
    {
    if(Xrm.Page.getAttribute("new_dateofbirth").getValue() != null)
    {
           var age;
           var now = new Date();
           var birthday = Xrm.Page.getAttribute("new_dateofbirth").getValue();
           var monthdif = now.getMonth() - birthday.getMonth();
           var nowyear = Xrm.Page.getAttribute("new_dateofbirth").getValue().getFullYear();
           if(monthdif > -1)
           {
                    age= now.getFullYear() - birthday.getFullYear();
        if(age < 18)
        {
           Xrm.Utility.alertDialog("Your Message",onCloseCallback);
        }
        else
        {
                    Xrm.Page.getAttribute("new_age").setValue(now.getFullYear() - birthday.getFullYear());
        }
           }
           else
           {
                age =now.getFullYear() - birthday.getFullYear()-1;
          if(age < 18)
        {
           Xrm.Utility.alertDialog("Your Message",onCloseCallback);
        }
                   Xrm.Page.getAttribute("new_age").setValue(now.getFullYear() - birthday.getFullYear()-1);
           }
    }
    }
     function onCloseCallback()
     {
     Xrm.Page.getAttribute("new_age").setValue(null);
     }

  • Community Member Profile Picture
    on at

    Thanks a lot Sartaj. Your code is working as per my requirement..

  • Sartaj Profile Picture
    880 on at

    That Cool , Vignesh.

    Please close this thread by marking that response as verified :)

    Sartaj

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans