How do I ensure one date field does not start before another date field?

kevin_eldridge
Kilo Guru

I have a requirement where one date field, End Date [endDate], should not start before the other date field on my form, Start Date [startDate].

I am trying to write a Catalog Client Script as this is for a Record Producer, but am having difficulty writing this script. Can anyone assist me with this issue?

Thank you,

Kevin E

1 ACCEPTED SOLUTION

Prateek kumar
Mega Sage

Hello Kevin


Try this.


Should be a Onsubmit client script


function onSubmit() {


//Type appropriate comment here, and begin script below


var startDate = g_form.getValue("start_date");


var endDate = g_form.getValue("end_date");



if (startDate == "" || endDate == "")


return;



if(startDate == endDate){


g_form.addErrorMessage('Start date must be before end date');


return false;


}



var result = compareDates(startDate, endDate);



if (startDate > endDate) {


g_form.addErrorMessage('Start date must be before end date');


return false;



}


}



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

4 REPLIES 4

Prateek kumar
Mega Sage

Hello Kevin


Try this.


Should be a Onsubmit client script


function onSubmit() {


//Type appropriate comment here, and begin script below


var startDate = g_form.getValue("start_date");


var endDate = g_form.getValue("end_date");



if (startDate == "" || endDate == "")


return;



if(startDate == endDate){


g_form.addErrorMessage('Start date must be before end date');


return false;


}



var result = compareDates(startDate, endDate);



if (startDate > endDate) {


g_form.addErrorMessage('Start date must be before end date');


return false;



}


}



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

I appreciate the quick response. I will give this a try and let you know if it works



Thank you,



Kevin E


Hi Kevin,



We have a solution for this in our instance but it is quite elaborate and not sure if it needs to be this elaborate but here it goes:



We have an include script that has a bunch of functions within it, the important one is 'getDateTimeDiff'


the code for it is here:


time diff.png



We call that function back in a client script and this is how we have the client script set up:



cleint script.png



The UX you get from this is when a user selects a date that is before the start date, it throws an error with the text you see in the Client script towards the bottom, when they click ok on the error message it clears the field so no value is stored.



Hope this helps


Thank you for your post as well Stephen! I used the first option and it worked. I will keep what you provided in my back pocket for later usage if needed.



Kevin