Date field Validation to restrict selecting past dates
This content was last updated in 2022. It may still be useful, but could contain outdated information. Please confirm its accuracy before relying on it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 05:08 AM
Hi Experts,
I have written a onchange catalog client script on the "Start Date" variable to restrict users from not to select the past dates. The below is the code I am using, But the code is also triggering if the selected date is today's date (current date). All I need to is restrict user to not to select any date before current date.
code:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type //get the entered date string
var enteredDate= g_form.getValue('start_date');
//get the current date/time
var rightNow = new Date().valueOf();
var dateToCheck = new Date(enteredDate).valueOf();
if (dateToCheck < rightNow)
{
//Display Error Message
g_form.addErrorMessage(getMessage('Select appropriate date'));
g_form.setValue('start_date','');
}
}
Any ideas on how to achieve this ?
Thanks in advance,
Rocky.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 05:13 AM
Hi,
Use this below code
Script Include:
Name:DateValidation
Client callable: Checked
var DateValidation = Class.create();
DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDate: function() {
var ActualEndDate = this.getParameter('sysparm_end_date');
return gs.dateDiff(gs.now(),ActualEndDate, true)/86400;
},
type: 'DateValidation'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('DateValidation');
ga.addParam('sysparm_name','validateDate');
ga.addParam('sysparm_end_date',g_form.getValue('u_business_need_by_date'));//give your date field
ga.getXML(ProcessResult);
function ProcessResult(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer < 0)
{
g_form.clearValue('u_business_need_by_date'); //give your date field
alert('Business need date should not be in the Past.');
}
}
}
Kindly mark it correct and helpful
Thanks,
Priyanka