- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2023 06:50 PM
Hi Team,
I have this code for validating Valid_to date in knowledge article and it is not entering into first if condition. Please suggest what went wrong.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var validToDate = g_form.getValue('valid_to');
var topic = g_form.getValue('topic');
var today = new Date();
alert(today);
var oneYearFromToday = new Date(today.getFullYear() + 1, today.getMonth(), today.getDate());
var sixMonthsFromToday = new Date(today.getFullYear(), today.getMonth() + 6, today.getDate());
alert(topic);
alert(oneYearFromToday);
alert(sixMonthsFromToday);
if (validToDate > oneYearFromToday) {
alert("valid to is more that 1 year");
g_form.showErrorBox('Invalid date', 'Valid to date cannot be more than 1 year in the future');
g_form.setValue('valid_to', oldValue);
return;
}
if (validToDate < today) {
g_form.showErrorBox('Invalid date', 'Valid to date cannot be a past date');
g_form.setValue('valid_to', oldValue);
return;
}
if (topic === 'Known Error' && (validToDate < today || validToDate > sixMonthsFromToday)) {
g_form.showErrorBox('Invalid date', 'Valid to date cannot be a past date or more than 6 months in the future for known errors');
g_form.setValue('valid_to', oldValue);
return;
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2023 07:19 PM
Hi @geogeorge ,
It is because of the type difference between the validToDate which you retrieved from the form and the oneYearFromToday is JavaScript date type.
Try converting the validToDate to JS date object type. Like below,
var validToDate = g_form.getValue('valid_to');
validToDate = new Date(validToDate + '');
Then try comparing them. And also check all the variables validToDate and oneYearFromNow are having the intended values by using an alert, before the if condition.
Thanks,
Anvesh
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2023 07:19 PM
Hi @geogeorge ,
It is because of the type difference between the validToDate which you retrieved from the form and the oneYearFromToday is JavaScript date type.
Try converting the validToDate to JS date object type. Like below,
var validToDate = g_form.getValue('valid_to');
validToDate = new Date(validToDate + '');
Then try comparing them. And also check all the variables validToDate and oneYearFromNow are having the intended values by using an alert, before the if condition.
Thanks,
Anvesh
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2023 12:46 AM - edited 04-18-2023 01:03 AM
HI @AnveshKumar M ,
Can we pass current date into a label field of UI builder