Set error message if the start date is greater than end date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 11:41 PM
Set error message if the start date is greater than end date.Please help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 11:50 PM
hi
Here is the sample script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var currentDate = new Date(); //Current Date
var selectedDate = new Date(g_form.getValue('u_date_field')); //Date field value
if (currentDate.getTime() >= selectedDate.getTime()) {
//Past Date on Date field should thrown an error near field
g_form.addErrorMessage('You can not select a Past Date');
return false;
}
}
Please check and let me know
Thanks 🙂
Shakeel Shaik 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2022 12:01 AM
Steps:
1. Create OnChange Client script on start date field.
2. scrip:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var endDate = new Date(); //Current Date
var startDate = new Date(g_form.getValue('u_start_date')); //Change value as per requirement.
if (endDate.getTime() > startDate.getTime()) {
g_form.addErrorMessage('You can not select a Future Date');
return false;
}
}
Please check and Let us know.
Thanks 🙂
Shakeel Shaik 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2022 11:56 PM
Hi, you can take inspiration from this ootb script on Change:
function onSubmit() {
var startDate = g_form.getValue("start_date");
var endDate = g_form.getValue("end_date");
var format = g_user_date_time_format;
if (startDate === "" || endDate === "")
return true;
// get date strings into a number of milliseconds since 1970-01-01
var startDateMs = getDateFromFormat(startDate, format);
var endDateMs = getDateFromFormat(endDate, format);
if (startDateMs < endDateMs)
return true;
g_form.clearMessages();
// 0 from "getDateFormat" means an invalid date string was passed to it
if (startDateMs === 0 || endDateMs === 0) {
if (startDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("start_date")));
if (endDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("end_date")));
return false;
}
if (startDateMs > endDateMs) {
g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("end_date"), g_form.getLabelOf("start_date")));
return false;
}
}
Best regards,
Sebastian Laursen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2022 12:07 AM
Hi,
you can simply use onChange client script OR onSubmit OR UI policy for this
onSubmit
function onSubmit() {
//Type appropriate comment here, and begin script below
g_form.hideErrorBox('start_date');
g_form.hideErrorBox('end_date');
if(g_form.getValue('start_date') != '' && g_form.getValue('end_date')){
var start = new Date(g_form.getValue('start_date')).getTime();
var end = new Date(g_form.getValue('end_date')).getTime();
if(end < start){
var message = 'Please give valid start and end dates';
g_form.showErrorBox('start_date', message);
g_form.showErrorBox('end_date', message);
return false;
}
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader