- 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 11:05 AM
Hi Nicole,
you need to do a GlideAjax to a script include where you can validate the date with the function gs.daysAgo(-14)) which gets the actual date and adds 14 days, if the field date entered is less than that you use the g_form.addInfoMessage() to pop up the alert and the g_form.setValue to wipe the mistake date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-05-2017 11:50 AM
Hi,
You can do this with the help of an Script Include and a On Change Client Script as mentioned below:
1) Create a Script Include with below code:
Script:
var DateValidation = Class.create();
DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDate3: function() {
var ActualEndDate = this.getParameter('sysparm_start_date');
return gs.dateDiff(gs.now(),ActualEndDate, true)/86400;
},
type: 'DateValidation'
});
2) Now create an On Change Client Script on the required Table and field to check on the same. For example The below script is to check for the Planned Start Date field on the Change Request form:
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('DateValidation');
ga.addParam('sysparm_name','validateDate3');
ga.addParam('sysparm_start_date',g_form.getValue('start_date'));
ga.getXML(ProcessResult);
function ProcessResult(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if (answer < 14)
{
g_form.clearValue('start_date');
alert('Start Date should not be more than 14 Days in the Past.');
}
}
}
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 04:31 AM
This is the script I currently have:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var cdt = g_form.getValue('start_date'); //first Date/Time field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeBeforeNow');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if (answer < 14){
g_form.clearValue('start_date');
alert("You've entered a start date less than two weeks prior to the Start Date of the course. We cannot support all the logistical requirements to ensure a successful delivery for this course.");
}
}
}
And this is what returns in the UI:
How can I fix this to return my message?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2017 04:33 AM
After I click 'Ok' on the message, the correct one appears with the text.