- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2017 09:37 AM
I need an 'OnChange' script that will check to see whether the entered 'start_date' that is less than 2 weeks from today (or whatever day they are filling out the form). If they enter a date less than 2 weeks away, I want to display a pop up message and prevent them from submitting the form.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2017 05:22 AM
Hi Nicole,
If you don't want to do ajax calls each time, you can use JavaScript validation in onChange client script and onSubmit script :
var date1 = new Date(g_form.getValue('start_date');
var date2 = new Date();
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
if(diffDays >= 14) {
alert("Error Message");
return false;
}
else
return true;
Regards,
Chirag Bagdai

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2017 12:00 PM
Hi Nicole,
Please check the script below.
Script Include:
var test = Class.create();
test.prototype = Object.extendsObject(AbstractAjaxProcessor, {
testing: function() {
var startDT = this.getParameter('sysparm_sdt');
var abc= gs.daysAgo(-14);
if (startDT > abc) {
return false;
} else {
return true;
}
},
getNowDateTime: function() {
return gs.nowDateTime();
}
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var sd=g_form.getValue('u_start_date');
alert(sd);
var startDate = newValue;
var ga = new GlideAjax('test');
ga.addParam('sysparm_name', 'testing');
ga.addParam('sysparm_sdt', sd);
ga.getXML(checkDate);
}
function checkDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'false') {
alert("please write your message ");
g_form.setValue('u_start_date', ''); }
}
Script Include must be client callable. and i just reset the field when ever user will try to select the date more than 14 days on the basis of current date.
you can try with another client side pop up functionality.
Thanks,
Harshvardhan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2017 04:57 AM
Simply comment out the alert statement before your if block i.e.if(answer<14) before this if block there is an alert message in Line Number 19.Remove it from your code or comment it out and this would resolve your issue. Please see the screen shot below:
Hope this helps.Mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2017 05:34 AM
Thank you, this worked to remove the extra message. However, the correct message is still displaying, even if I select a date more than 2 weeks from now (even if a full month from now).

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2017 08:48 AM
have you tried my script?
i tested in my demo instance and it is working fine.
Thanks,
Harshvardhan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2017 11:18 AM