Conditional Field If Less than 18 Years Old

  • multiweb
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 11
  • Thank you received: 0

multiweb created the topic: Conditional Field If Less than 18 Years Old

Hi Guys,

Hoping I can get some help on creating a conditional field that displays once a user enters that Date Of Birth (using the Responsive Calendar Element) and is under the age of 18.

I have created the form with the Conditional field turned off.

How do I go about checking the age field and then displaying the conditional field?

Thanks
#236143
  • Posts: 2275
  • Karma: 84
  • Thank you received: 305

mihaela replied the topic: Conditional Field If Less than 18 Years Old

Hello,

Here is how you can make this work as you wanted. This example is for date format dd/mm/yyyy.

Please go to form's Advanced properties > More options > Scripts and in the Initialization script set Type to Custom.

In the code area below put the following code:
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;
  }
    if(age <18) {
        bfToggleFields('on','element','FIELDNAME',bfDeactivateField);
    }
   else if(age >= 18)  {
        bfToggleFields('off','element','FIELDNAME',bfDeactivateField);
    }

}
NOTE: In the code above you have to replace FORMNAME with the name of your form.
Also, you must replace dateField with the Name of the calendar element in your form. Also, replace FIELDNAME with the name of that conditional field that should display if age is < 18.

In order for this showing and hiding to work, you must write at least one visibility rule like:
turn on element mydummy if yourdummy is something
as explained in this piece of documentation crosstec.org/en/support/online-documenta...s-in-javascript.html .

Let me know whether you have managed to apply this to your form and whether it works as you wanted.

Regards,

Mihaela
#236151
  • multiweb
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 11
  • Thank you received: 0

multiweb replied the topic: Conditional Field If Less than 18 Years Old

Hi Michaela,

Thank you very much for your reply.

I am struggling with the very last part, what to write for the visibility rule. I have updated the provided script to include the names of my form elements, where registration is the name of the form, dobfield is the name of the Responsive Calendar and legalguardian is the name of the checkbox element I would like to show if the user is less than 18 years of age:
function ff_registration_init()
{
setInterval(calAge, 500);
} // ff_registration_init

function calAge(){
var dateBirth = ff_getElementByName('dobfield').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;
  }
    if(age <18) {
        bfToggleFields('on','element','legalguardian',bfDeactivateField);
    }
   else if(age >= 18)  {
        bfToggleFields('off','element','legalguardian',bfDeactivateField);
    }

}

And have added the following visibility rules:
turn on element legalguardian if dobfield is on
turn off element legalguardian if dobfield is off

The legalguardian checkbox element is originally set to be turned off when the form loads, however when the Date of Birth is selected using the Responsive Calendar the legalguardian element does not display.

Please could you help me to complete this action?
#236165
  • Posts: 2275
  • Karma: 84
  • Thank you received: 305

mihaela replied the topic: Conditional Field If Less than 18 Years Old

Hello again,

Please, for the visibility rules try to write literally:
turn on element mydummy if yourdummy is something

I've tested your code and in my case it worked properly even with the two visibility rules that you've set.

So when a date form let's say 2015. is selected, the checkbox shows and when a date from 1990. is selected it is turned off(hidden).

Let me know whether changing the visibility rules made it work.

Regards,

Mihaela
#236183
  • multiweb
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 11
  • Thank you received: 0

multiweb replied the topic: Conditional Field If Less than 18 Years Old

Hi Mihaela,

Thank you for your reply.

I changed the visibility rule to no avail, still nothing happens when the age is selected.

Any idea where I should look?
#236201
  • Posts: 2275
  • Karma: 84
  • Thank you received: 305

mihaela replied the topic: Conditional Field If Less than 18 Years Old

Hello again,

In that case could you please create a package with your form as explained here crosstec.org/en/support/online-documenta...ted/94-packages.html , zip it and send as an attachment.

I want to look at this from first hand to see what might cause this not to work in your form.

Regards,

Mihaela
#236209
Moderators: ForumSupporttomeperica
Time to create page: 0.068 seconds

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

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€

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!

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