× Form Help

Age Restrict

  • nicko22
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 10
  • Thank you received: 0

nicko22 replied the topic: Age Restrict

Hi Mihaela,

This is working great thank you.

Is there a way to expand on this my having an element become visible when the date of birth is out of the range.

For example:
- they are too old or too young
- a message appears (element) saying you are outside of our criteria.

Thanks
Nick
#236240
  • Posts: 2478
  • Karma: 86
  • Thank you received: 333

mihaela replied the topic: Age Restrict

Hello again,

Yes, there is a way to make this work as you want.

The simplest solution for this is to use this code as the validation script for the calendar element.

So, instead of using this script in Initialization script area, go to Properties of the calendar element. In the Validation section check the "Required" checkbox and set the Validation Type to "Custom".

In the code area below put the following code:
function ff_dateField_validation(element, message)
{
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 < 7 || age > 8) {
        if (message=='') message = "Entered age should be between 7 and 8.\n"
        ff_validationFocus(element.name);
        return message;
    } // if
    else if(age == 7 || age == 8) { return '';}
    else {
        if (message=='') message = "Entered age should be between 7 and 8.\n"
        ff_validationFocus(element.name);
        return message;
    }
} // ff_dateField_validation
NOTE: In the code above you have to replace dateField with the Name of the calendar element in your form.

That should do the trick. This way user shouldn't be able to submit the form unless age is 7 or 8.

Let me know whether you've managed to set this for your form.

Regards,

Mihaela
#236264
  • nicko22
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 10
  • Thank you received: 0

nicko22 replied the topic: Age Restrict

Brill - thanks again for your support :)
#236271
  • Posts: 2478
  • Karma: 86
  • Thank you received: 333

mihaela replied the topic: Age Restrict

I'm glad I've helped and that it works as wanted. :)

Regards,

Mihaela
#236272
  • Posts: 5
  • Thank you received: 0

ideasgirl replied the topic: Age Restrict

Hi, I've tried this and changing it to my requirements the form doesn't do anything. This is my code changed to my needs:
function ff_dateField_validation(element, message)
{
var dateBirth = ff_getElementByName('fecha_nacimiento').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 || age > 85) {
        if (message=='') message = "Debes ser mayor de 18 años.\n"
        ff_validationFocus(element.name);
        return message;
    } // if
    else if(age == 18 || age == 85) { return '';}
    else {
        if (message=='') message = "Debes ser mayor de 18 años.\n"
        ff_validationFocus(element.name);
        return message;
    }
} // ff_dateField_validation

I'm just doing the validation part as you suggested.

I just need that the person filling up the form to be 18 or older.
#236809
  • Posts: 2478
  • Karma: 86
  • Thank you received: 333

mihaela replied the topic: Age Restrict

Hello,

There are two reasons why this might not be working for you.

First is that you are not using the correct date format for which this code is written.
The calendar element must represent dates in format dd/mm/yyyy.

Second thing is that these validation messages are displayed after the submit button is pressed. So you have to enter a date so that age is not allowed and press the submit button. Then you should see the error message.

Let me know whether this resolves the issue.

Regards,

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

New Icon Packs Category!

Crosstec is now offering icon packs.

If you are a paying subscriber, icon packs are automatically added to your account.

Check out our icon packs page!

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 and icon packs!

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