- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2016 01:15 AM
Hi,
I've the following script that allows not to select any past dates. But I would like to modify this to select the current date and any future dates. Please advise. Thanks ! function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue == '') { return; } var d = getDateFromFormat(newValue, g_user_date_format); var today = new Date(); if (d < today) { alert('The date value cannot be before the current date and time. Please correct.'); g_form.clearValue("effective_date"); } }
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2016 04:08 AM
Hi Imran and All,
Let us make this very very simple.
We will make a UI Policy with this condition :
here manufacture_date is the field which I'm checking if a past date is not entered. Replace it with your own variable name.
And now we just write a 2 line code in the "Script" part of the same UI Policy :
And that's it guys. We are good to go. We need not use any client scripts because for form validations the best practice is to use UI Policies. Using client scripts would have performance issues.
Please let me know if you have further doubts.
Thanks,
Arnab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2016 01:33 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2016 01:45 AM
Here you go:
Script Include:
Name: ClientDateTimeUtils
Client Callable: True
Code:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getNowDateTimeDiff: function(){
var firstDT = this.getParameter('sysparm_fdt'); //First Date-Time Field
var diff = gs.dateDiff(gs.nowDateTime(), firstDT, true);
return diff;
},
type: 'ClientDateTimeUtils'
});
Now the onChange Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('effective_date'); //First Date/Time field
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer < 0)
{
alert('The date value cannot be before the current date and time. Please correct.');
g_form.clearValue("effective_date");
}
}
}
Let me know the result.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2016 02:54 AM
Sorry, it didn't work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-16-2016 02:54 AM
Sorry, it didn't work.