Date validation - client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 06:59 PM
We have a requirement for a date variable on record producer producer name: date_required to only allow saturdays to be selected. Is there any way to do this via an on change client script?
Tried to use the below on change script but didn't work
Thanks
Sean
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 07:56 PM
Hi @Sean50
You can leverage the GlideDateTime API
getDayOfWeek()
1. Create a Client callable script include.
var CLCatalogItemUtilAJAX = Class.create();
CLCatalogItemUtilAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isSaturday: function(){
var gdt = new GlideDateTime();
return gdt.getDayOfWeekLocalTime() == 6;
},
type: 'CLCatalogItemUtilAJAX'
});
2. Make an Ajax call to the function isSaturday above in your client script.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('date_required', true);
var ga = new GlideAjax('CLCatalogItemUtilAJAX');
ga.addParam('sysparm_name', 'isSaturday');
ga.getXMLAnswer(function(answer){
if(answer == false){
g_form.showFieldMsg('date_required', 'ERROR: Date selected must be on a Saturday', 'error', false);
}else{
g_form.showFieldMsg('date_required', 'Date is OK', 'info', false);
}
});
}
Let me know if it works for you.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2024 08:07 PM
Hi Tai
Tried this out but doesn't seem to do anything on the frontend form. When I look at the record producer preview area I can see that the message date is okay is popping up when any date is entered so something is happening but it isn't working properly.
Any ideas on how to debug it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2024 08:16 PM
Hi @Sean50
Oops, we need to pass the selected date to the function when we initiate the GlideDateTime and it should do the trick.
var CLCatalogItemUtilAJAX = Class.create();
CLCatalogItemUtilAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isSaturday: function(){
var selectedDate = this.getParameter('sysparm_selected_date');
var gdt = new GlideDateTime(selectedDate);
return gdt.getDayOfWeekLocalTime() == 6;
},
type: 'CLCatalogItemUtilAJAX'
});
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.hideFieldMsg('date_required', true);
var ga = new GlideAjax('CLCatalogItemUtilAJAX');
ga.addParam('sysparm_name', 'isSaturday');
ga.addParam('sysparm_selected_date', newValue); //your date variable
ga.getXMLAnswer(function(answer){
if(answer == false){
g_form.clearValue('date_required');
g_form.showFieldMsg('date_required', 'ERROR: Date selected must be on a Saturday', 'error', false);
}else{
g_form.showFieldMsg('date_required', 'Date is OK', 'info', false);
}
});
}
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2024 08:02 PM
Hi @Sean50
You can use the following script, if the date selected is not a Saturday, clear the value and display error message at form level. If you use field message it will not display the error, as we are clearing the value.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var date_number = getDateFromFormat(newValue, g_user_date_format);
var my_date = new Date(date_number);
var msg;
if(today.getDay() == 6){
msg = 'Date is OK';
g_form.hideFieldMsg('date_required');
g_form.showFieldMsg('date_required',msg,'info');
} else {
msg = 'ERROR: Date selected must be on a Saturday';
g_form.hideFieldMsg('date_required');
g_form.setValue('date_required', '');
g_form.addErrorMessage(msg);
}
}
Please mark my answer helpful and accept as a solution if it helped 👍✅
Anvesh