Restrict end date to be after start date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-05-2018 04:49 PM
Hello all,
I have a script that I got from another thread to restrict the end date to be selected after the start date.
My issue is that it doesn't allow users to select the present date so I want to see if it is possible to exclude past dates but only after the present date.
For example:
Start Date 12/25/18
End Date 12/25/18 (it should exclude 12/24/18 and anything before that)
My Catalog Client Script below:
Type: onChange
function onChange(control, oldValue, newValue, isLoading)
{
if(isLoading)
{
return;
}
//start the validation
if(newValue != '')
{
var ga = new GlideAjax('ConfirmDate');
ga.addParam('sysparm_name', 'chkCurrDate');
ga.addParam('sysparm_date',g_form.getValue('end_vacation'));
ga.getXML(NewParse);
}
function NewParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
alert("Date selected is a past entry.");
g_form.setValue('end_vacation', ''); // Setting the variable as empty
}}}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2018 10:51 AM
Thank you Jon I'm going to give it a try. Just to make sure when replacing your placeholders with my field names, I see CompareValue and CompareName this is where I would specify start date and end date right? or do I need a ui script for each?
Thanks,
grace
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2018 11:11 AM
I have the below not sure if I'm doing it right:
function ValidateDateGreaterThanOtherDate(StartDate, start_vacation, EndVacation, end_vacation, EndDate) {
var Format = g_user_date_format;
if (StartDate == '') {
g_form.clearValue(EndVacation);
g_form.showFieldMsg(EndVacation, start_vacation + ' must be entered before ' + EndVacation, 'error', true);
}
else {
g_form.hideFieldMsg(TheField);
var StartDateNum = getDateFromFormat(start_vacation, Format);
var EndVacationNum = getDateFromFormat(end_vacation, Format);
if (EndVacationNum < StartVacationNum) {
g_form.clearValue(end_vacation);
g_form.showFieldMsg(end_vacation, EndDate + ' must be later than ' + StartDate, 'error', true);
}
else
g_form.hideFieldMsg(EndDate);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2018 11:32 AM
There is just one UI Script involved in validating the end date is after the start date. I have another to check to ensure the start date is not before the current date.
CompareValue is the value of start_date you’re passing in. TheField is the name of the end_date field. TheValue is the value of end_date.
You actually could have left the variable names the same in the UI script. What matters is what you're passing in from the catalog client script
In your script above: else { g_form.hideFieldMsg(TheField); should be hiding EndVacation as there is no TheField variable declared.
This line should be using StartDate - var StartDateNum = getDateFromFormat(start_vacation, Format);
Both of the following should be using EndVacation
g_form.clearValue(end_vacation);
g_form.showFieldMsg(end_vacation, EndDate + ' must be later than ' + StartDate, 'error', true);
else
g_form.hideFieldMsg(EndDate);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-06-2018 11:38 AM
While this would get the job done, I'm not sure why you wouldn't just use a script include. Instead of managing your code in two separate UI Scripts you could have both types of validation within one script include. It's just as reusable as a UI Script and is arguably easier to maintain.