Age from

  • Eddy.vh
  • Eddy.vh's Avatar Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 158
  • Karma: 1
  • Thank you received: 14

Eddy.vh created the topic: Age from

Hello.

I need a little help again.
1 Text field with validation of the format Date dd / mm / yyyy
1 read-only field that should display the age of the person (in years, months, days) after entering their date of birth in field 1
I do not see how I can write the script that can do this calculation in real time.

Thank you very much for any idea.
#235345
  • mihaela
  • Away
  • Administrator
  • Administrator
  • Posts: 2360
  • Karma: 85
  • Thank you received: 316

mihaela replied the topic: Age from

Hello,

I will just check one thing before giving you instructions how to achieve what you want.

If I understood correctly, the date in format dd/mm/yyyy will be entered in "Textfield" and not in "Calendar" element, is that correct?

Regards,

Mihaela
#235352
  • Eddy.vh
  • Eddy.vh's Avatar Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 158
  • Karma: 1
  • Thank you received: 14

Eddy.vh replied the topic: Age from

Hi Mihaela

Yes, in a text field at best, this field must fill dynamically as soon as you leave the field. But, it may be that the calendar is chosen as an option.

Will the approach be very different? I look for myself, I found a few things but I can not energize the field of age.

Thank you for your ever precious help
#235353
  • mihaela
  • Away
  • Administrator
  • Administrator
  • Posts: 2360
  • Karma: 85
  • Thank you received: 316

mihaela replied the topic: Age from

Hello,

To calculate age from the birth date please do as follows.

Go to Advanced properties of the textfield where the date will be filled in.
In Actionscript section set Type to Custom and Action to Change.
Then in the code area below put the following code:
function ff_dateField_action(element, action)
{   

var dateBirth = ff_getElementByName('dateField').value;
    var dobParts = dateBirth.split("/");
     var now = new Date();
  var dobParts = dateBirth.split("/");
  var dob = new Date(dobParts[2], (dobParts[1] - 1), dobParts[0]);
  
  var dobYear = dob.getFullYear();
  var dobMonth = dob.getMonth();
  var dobDay = dob.getDate()

  var age = now.getFullYear() - dobYear;
  var ageMonth = now.getMonth() - dobMonth;
  var ageDay = now.getDate() - dobDay;

  if (ageMonth < 0 || (ageMonth == 0 && ageDay < 0)){
    age = parseInt(age) - 1;
  }

    ff_getElementByName('age').value = age;
} // ff_dateField_action
NOTE: In the code above you have to replace dateField with the Name of your calendar element.
Also, in ff_getElementByName('age').value replace age with the name of the field where date should be displayed.

In case you want to use a calendar element instead of textfield, since calendar element doesn't have Actionscript functionality, you can put the script to calculate age in Initialization script like this:
function ff_formname_init() {
  setInterval(calAge, 500);
} // ff_formname_init

function calAge(){
var dateBirth = ff_getElementByName('dateField').value;
    var dobParts = dateBirth.split("/");
     var now = new Date();
  var dobParts = dateBirth.split("/");
  var dob = new Date(dobParts[2], (dobParts[1] - 1), dobParts[0]);
  
  var dobYear = dob.getFullYear();
  var dobMonth = dob.getMonth();
  var dobDay = dob.getDate()

  var age = now.getFullYear() - dobYear;
  var ageMonth = now.getMonth() - dobMonth;
  var ageDay = now.getDate() - dobDay;

  if (ageMonth < 0 || (ageMonth == 0 && ageDay < 0)){
    age = parseInt(age) - 1;
  }

    ff_getElementByName('age').value = age;
}

Let me know if this is what you wanted to achieve and whether you have managed to implement this to your form.

Regards,

Mihaela
#235406
  • Eddy.vh
  • Eddy.vh's Avatar Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 158
  • Karma: 1
  • Thank you received: 14

Eddy.vh replied the topic: Age from

Hi Mihaela.

It's great. I thank you very much for your help. The script on the text field works perfectly.
The script on the calendar, although the script is almost identical, is an error. See screenshot.
#235421
Attachments:
  • Eddy.vh
  • Eddy.vh's Avatar Topic Author
  • Offline
  • Expert Boarder
  • Expert Boarder
  • Posts: 158
  • Karma: 1
  • Thank you received: 14

Eddy.vh replied the topic: Age from

Sorry Mihaela.

My mistake, I wrongly changed a value, both solutions work perfectly.
I thank you for the time spent helping us and always appreciate your help.
Have a good day.

Best regards.
#235425
Moderators: ForumSupporttomeperica
Time to create page: 0.069 seconds

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Summer Sale!

50% discount on all of our extension subscription plans & templates!

Get Your Subscription Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99 for just 49! (Summer Sale)

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Live Support Chat Opened!

Join our Discord chat here to receive live support and talk directly to the team!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here