Add Months to Date

  • emtbrian2000
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 18
  • Thank you received: 0

emtbrian2000 created the topic: Add Months to Date

Greetings,

I would like to add a user entered number of months to date and pass that to a hidden field to calculate the end date. Ex:

Purchase Date: 05/01/2020
Warranty: 12 (Months)
Warranty Expiration (Calculated): 06/01/2020

I have this code in my validation custom field for the hidden expiration date field:
function ff_warrantyexpiration_validation(element, message)
{
    pd = ff_getElementByName('purchdate').value;
	wl = ff_getElementByName('warrantylength').value;
    newdate = new Date().setMonth(pd.getMonth() + wl);
	ff_getElementByName('warrantyexpiration').value = newdate;
    return '';
} // ff_warrantyexpiration_validation

When I try to submit my form it just hangs. Any help would be greatly appreciated. Thank you.
#240902
  • TheMuffinMan
  • TheMuffinMan's Avatar
  • Away
  • Administrator
  • Administrator
  • Posts: 8403
  • Karma: 164
  • Thank you received: 669

TheMuffinMan replied the topic: Add Months to Date

Hi,

I think the calculation itself is almost ok but the bigger problem is the fact that you are trying to achieve this using a validation.

Please use Action Script in the advanced tab using the "change" event and perform your calculations and field assignments there.

I am pretty sure this will solve the hanging problem (as long as there are no syntax issues, which I cannot see there).

Regards,
Markus

===========================
If you are happy, please leave us a small review here to keep us motivated:
(Please turn off your VPN and local Apache prior reviewing as your review might get blocked)

⭐⭐⭐⭐⭐ BreezingForms: extensions.joomla.org/extension/contacts...orms/breezing-forms/
⭐⭐⭐⭐⭐ ContentBuilder: extensions.joomla.org/extension/authorin...tion/contentbuilder/

===========================
Satisfied?
Get a membership!
===========================
#240939
  • emtbrian2000
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 18
  • Thank you received: 0

emtbrian2000 replied the topic: Add Months to Date

Thank you. The advanced tab for the hidden element? I only see an "Initscript" there. Can you elaborate on where I can find the advanced tab and change event please?

Also it seems I posted this in the free section by mistake and not the subscriber section. Can it be moved? Thanks!
#240975
  • TheMuffinMan
  • TheMuffinMan's Avatar
  • Away
  • Administrator
  • Administrator
  • Posts: 8403
  • Karma: 164
  • Thank you received: 669

TheMuffinMan replied the topic: Add Months to Date

Hi,

sure, please have a look at the screenshot.



The exact location is "your element" => advanced => scroll to bottom of the settings => Actionscript.

There click on custom => check "change" (because we want to execute custom code when something changes) => click on create code framework.

Inside the generated function skeleton, right after 'case "change":' you'd have to place your custom logic that you previously tried to achieve with a validation.

Regards,
Markus

===========================
If you are happy, please leave us a small review here to keep us motivated:
(Please turn off your VPN and local Apache prior reviewing as your review might get blocked)

⭐⭐⭐⭐⭐ BreezingForms: extensions.joomla.org/extension/contacts...orms/breezing-forms/
⭐⭐⭐⭐⭐ ContentBuilder: extensions.joomla.org/extension/authorin...tion/contentbuilder/

===========================
Satisfied?
Get a membership!
===========================
#241021
Attachments:
  • TheMuffinMan
  • TheMuffinMan's Avatar
  • Away
  • Administrator
  • Administrator
  • Posts: 8403
  • Karma: 164
  • Thank you received: 669

TheMuffinMan replied the topic: Add Months to Date

Hi,

I think I did a mistake, please disregard my previous post.

Instead, please apply the following validation to your hidden input (you actually had the better idea):
function ff_NAMEOFHIDDENFIELD_validation(element, message)
{

    element.value = ff_getElementByName('NAMEOFCALENDAR');
    
    if (element.value == '') {
        if (message=='') message = element.name+" faild in my test.\n"
        ff_validationFocus(element.name);
        return message;
    } // if
    return '';
}

Please replace NAMEOFHIDDENFIELD with the name (not title) of the hidden input and replace NAMEOFCALENDAR with the name of the calendar/date field (not title).

This will then copy the calendar value to the hidden field and apply a validation at the same time.

Regards,
Markus

===========================
If you are happy, please leave us a small review here to keep us motivated:
(Please turn off your VPN and local Apache prior reviewing as your review might get blocked)

⭐⭐⭐⭐⭐ BreezingForms: extensions.joomla.org/extension/contacts...orms/breezing-forms/
⭐⭐⭐⭐⭐ ContentBuilder: extensions.joomla.org/extension/authorin...tion/contentbuilder/

===========================
Satisfied?
Get a membership!
===========================
#241026
  • emtbrian2000
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 18
  • Thank you received: 0

emtbrian2000 replied the topic: Add Months to Date

Thanks for the quick reply. That is certainly getting me in the right direction. Integrating with your code I used this:
function ff_warrantylength_action(element, action)
{
    switch (action) {
        case 'change':
	pd = ff_getElementByName('purchdate').value;
	wl = ff_getElementByName('warrantylength').value;
	m = pd.getMonth();
    expdate = pd.setMonth(m + wl);
	ff_getElementByName('warrantyexpiration').value = expdate;
			return '';
            break;
        default:;
    } // switch
} // ff_warrantylength_action

Using that exact code does not work. The "Next" button acts as if though it was not clicked. Playing around with the code it seems to be these two lines that hang:
m = pd.getMonth();
expdate = pd.setMonth(m + wl);

Is the date format perhaps not correct to be able to use getMonth or setMonth? Thanks again.
#241057
Moderators: ForumSupport
Time to create page: 0.098 seconds

We Are Doing Our Part

Massive discounts on all of our extension subscription plans to help you and your business in these times.

Get Your Subscription Here

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!

News and Updates

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

All Extensions Subscription

We are doing our part and support your business with extremely low prices -- For example 6-Months-Subscription for only $9 USD. For as long as necessary.

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!