Date of birth javascript condition

leblancphil

Member
Hello dear developpers

I need a javascript condition in my date element to show "parents name elements" when age less than 18.

Could you help

Thanks
 
There's no built in way to do that, so you'd have to roll your own code. Create a ./components/com_fabrik/js/form_X.js file (replace X with numeric ID of your form), and create a function something like this ...

Code:
function doAge(el) {
   var myDate = new Date(el.getValue());
   var ageDiff = Date.now() - myDate.getTime();
   var ageDate = new Date(ageDiff);
   var age = Math.abs(ageDate.getUTCFullYear() - 1970);
   if (age < 18) {
      el.form.formElement.get('yourtable___parent_name1').show();
      el.form.formElement.get('yourtable___parent_name2').show();
   }
   else
   {
      el.form.formElement.get('yourtable___parent_name1').hide();
      el.form.formElement.get('yourtable___parent_name2').hide();
   }
}

Obviously substitute your element names that need to be shown/hidden, and add any that need it.

Then in your date element, add a JS event, onChange, that just does:

Code:
doAge(this);

Note that I got that code for getting age from date from Stack Overflow:

http://stackoverflow.com/questions/4060004/calculate-age-in-javascript

... if that particular recipe doesn't do what you want, pick another one. It seems to work, although I only tested a handful of simple case (prior to 1970, after 2000, today's date 18 years ago).

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top