× Form Help

Compare two calendar field values

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

hunkiedunkie created the topic: Compare two calendar field values

Hi,

I have added 2 Calendar Elements and would like to be able to validate the second Calendar Element.

The validation would be that the 2nd Calendar's Date and Time is after the first's.

Is this possible?
#234210
  • Posts: 3109
  • Karma: 104
  • Thank you received: 409

mihaela replied the topic: Compare two calendar field values

Hello,

You can take a look at this topic to see how to check if end date is after the start date as you want crosstec.org/en/forums/1-forums/123319-b...nd-datum.html#222360 .

To compare date and time elements in format "%Y-%m-%d %H:%M", do as follows.

In Validation section of 2nd Calendar Date and Time element set Validation Type to Custom and in the code area below put the following code:
function ff_cal2_validation(element, message)
{
    var Date1 = Date.parse(ff_getElementByName("cal").value); 

    var Date2 = Date.parse(element.value); 
    if ( Date1 >= Date2) {
        if (message=='') message = "Check out date must be after check in date.\n"
        ff_validationFocus(element.name);
        return message;
    } // if
    return '';
} // ff_cal2_validation
NOTE: In the code above you have to replace every instance of "cal2" with the Name of the 2nd calendar element and every instance of "cal" with the Name of the 1st calendar element

Let me know whether it works for you.

Regards,

Mihaela
#234217
  • hunkiedunkie
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 10
  • Thank you received: 0

hunkiedunkie replied the topic: Compare two calendar field values

Thank you. That code works in the %Y-%m-%d %H:%M format.

But when I change to %d-%m-%Y %H:%M format is does not work right. I've looked at the other topic too but am unsure where to put in the new code and what to enter.

Please can you help?
#234229
  • Posts: 3109
  • Karma: 104
  • Thank you received: 409

mihaela replied the topic: Compare two calendar field values

Hello,

The approach in code for %d-%m-%Y %H:%M format is different than in %d-%m-%Y %H:%M.

Please take a look at this example for format %d.%m.%Y %H:%M crosstec.org/en/forums/35-form-help/1259...-time-in-future.html .

If you have some programming knowledge you can adjust it for your example. In the code just replace "." separator with "-". Also, instead of comparing the date with today's date create the date element from second calendar element as well and then compare it.

Let me know whether you have managed to do so.

Regards,

Mihaela
#234232
  • hunkiedunkie
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 10
  • Thank you received: 0

hunkiedunkie replied the topic: Compare two calendar field values

Hi Mihaela,

Programming is not my strong area!

Based upon the example and the previous code advice, I tried the following but fear I may be well off as my form does not recognise it!

function ff_Calendar2_validation(element, message)
{
var Date1 = Date.parse(ff_getElementByName("Calendar1").value);
var Date2 = Date.parse(element.value);
var tim = element.value.split(" ")[1];
if(tim.split(":").length != 2) {return message == '' ? "Please select time.n" : message}
var dat = element.value.split(" ")[0];
var Date1 = new Date(Number( dat.split("-")[2]),Number(dat.split("-")[1]-1),Number(dat.split("-")[0]),Number(tim.split(":")[0]),Number( tim.split(":")[1]),0).getTime();
var Date2 = new Date(Number( dat.split("-")[2]),Number(dat.split("-")[1]-1),Number(dat.split("-")[0]),Number(tim.split(":")[0]),Number( tim.split(":")[1]),0).getTime();

if ( Date1 >= Date2) {
if (message=='') message = "Check out date must be after check in date.\n"
ff_validationFocus(element.name);
return message;
} // if
return '';
} // ff_Calendar2_validation

Please can you help?
#234239
  • Posts: 3109
  • Karma: 104
  • Thank you received: 409

mihaela replied the topic: Compare two calendar field values

Hello,

You were on a right track, but there are some errors in your code and missing parts.

Try using the following code:
function ff_Calendar2_validation(element, message)
{

// first calendar
    var tim1 = ff_getElementByName('Calendar1').value.split(" ")[1];
    if(tim1.split(":").length != 2) {return message == '' ? "Please select time.n" : message}
   
    var dat1 =  ff_getElementByName('Calendar1').value.split(" ")[0];

    var vDate1 = new Date(Number( dat1.split("-")[2]),Number(dat1.split("-")[1]-1),Number(dat1.split("-")[0]),Number(tim1.split(":")[0]),Number( tim1.split(":")[1]),0).getTime();

// second calendar
    var tim = element.value.split(" ")[1];
    if(tim.split(":").length != 2) {return message == '' ? "Please select time.n" : message}
   
    var dat = element.value.split(" ")[0];
    var vDate = new Date(Number( dat.split("-")[2]),Number(dat.split("-")[1]-1),Number(dat.split("-")[0]),Number(tim.split(":")[0]),Number( tim.split(":")[1]),0).getTime();


    if( vDate1 < vDate) {
          if (message=='') message = "End time must be after start time.n";
          ff_validationFocus(element.name);
          return message;
     }
    return '';
} // ff_Calendar2_validation
NOTE: Replace every instance of Calendar2 with the name of the second calendar element. Furthermore, replace every instance of Calendar1 with the name of the first calendar element.

That should compare dates in format %d-%m-%Y %H:%M .

Let me know whether it works as you wanted.

Regards,

Mihaela
#234262
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