Date validation on a variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2024 04:59 PM
Hi all
there is a variable on the form which should allow the date selection between 5 day prior and 2 weeks ahead from now.
how can i do that ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2024 06:26 PM
Hi @Kanika4 ,
Please try with the Catalog UI policies which requires no coding as instructed below:
No Code date validations through (Catalog) UI Policies
Mark this as Helpful / Accept the Solution if this helps
Mark this as Helpful / Accept the Solution if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2024 07:01 PM
Hi @SN_Learn,
Thank you for letting know about this article 😊. Haven't known we could achieve this with UI Policies, will try to implement this in future date validations.
Tried this for the above requirement and it worked.
@Kanika4 - Make sure to uncheck the On load checkbox. And write the clear value line of code prior, if you are using show field message method.
Mark the response correct and helpful if the answer assisted your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2024 06:31 PM - edited 07-07-2024 06:33 PM
Hi @Kanika4,
If you want to display an error message as soon as the date is selected on the variable. You need to write an onChange Client Script and Script Include for validation.
Please find the below scripts:
OnChange Client Script: (on change of the field/variable - date field)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var gaChng = new GlideAjax('Test'); //script include name
gaChng.addParam('sysparm_name', 'dateValidation'); //second parameter - function name in the SI
gaChng.addParam('sysparm_date', newValue);
gaChng.getXML(getData1);
function getData1(response) {
var answer1 = response.responseXML.documentElement.getAttribute('answer');
if (answer1 == 'false') {
g_form.clearValue('<field_name>'); //replace <field_name> with the field/variable name
g_form.showFieldMsg('<field_name>', '<add your error msg here>', 'error'); //replace <field_name> with the field/variable name
}
}
}
Script Include: (make client callable checkbox : true)
var Test = Class.create();
Test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
dateValidation: function() {
try {
var selected_date = new GlideDateTime(this.getParameter('sysparm_date'));
var flag = true;
selected_date = selected_date.getDate();
var gdt1 = new GlideDate();
gdt1.addDays(14);
var gdt2 = new GlideDate();
gdt2.addDays(-5);
if (selected_date.after(gdt1) || selected_date.before(gdt2)) {
flag = false;
}
return flag;
} catch (ex) {
gs.info('Error:' + ex.string() + '\nLine:' + ex.lineNumber);
}
},
type: 'Test'
});
Make sure to change the Script Include name, function name and field/variable names in Client script.
Mark the response correct and helpful if the answer assisted your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-07-2024 07:01 PM
Hi This is not working, The only dates I can select after implementing this code is 07/07/2024 and 22/07/2024.
It throws errors for all the dates which are 5 days prior today (03/07/2024...... 06/07/2024) and also cant select any date after 22/07/2024