Skip to main content
Participant
July 27, 2016
Question

Validate if the second date is in the same year as the first date.

  • July 27, 2016
  • 1 reply
  • 446 views

Hi.

I've created a form in which (among other things) the user has to input two dates, a start date and an end date.

From those two dates the number of weeks worked is calculated.

There is already a custom validation script in place which checks if the date is entered in the right formatting (day, month, year) and gives an error if that's not the case.

What I would like the script to do as well, is to check if the end date is in the same year as the start date.

I've found some examples of scripts that check the date, but can't figure out how to modify and add this in my own script.

This is the script thats in place now:

// JavaScript code for date mask format DD/MM/YYYY

var format = /^[mdy0-9]{2}\/[mdy0-9]{2}\/[mdy0-9]{4}$/

    var datespan = util.scand("dd/mm/yyyy", event.value);

//Allow blank space in field

if (event.value !="") {

  if (format.test(event.value) == false) {

    app.alert ({

       cTitle: "Ongeldige datum ingevuld.",

       cMsg: "De datum moet u als volgt invullen: dd/mm/jjjj."

    });

    }

}

(I've also got a document javascript and a custom keystroke script in place since this script doesn't seem to catch all the wrong date formats.)

Does anyone here have an idea how to check the second year against the first, give an error if it's not the same and how to add it to this script?

Thanks for any thoughts on this!

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
July 27, 2016

Read the value of the other field, convert it to a Date object as well, and then use the getFullYear method on both of the Date objects to make sure they fall within the same calendar year. If not, reject the new value and show an error message.

Participant
August 16, 2016

Hello try67, thanks for your answer, and sorry for the late reply. Something came up.

I understand the logic, but I'm not really sure on how to do that I'm afraid. I might get the script working on itself, but wouldn't know how to make it part of the first script. Is that something you could help me with?

try67
Community Expert
Community Expert
August 16, 2016

Sure. I sent you an email regarding it.