I want to add validation on date and time on a record producer

Muhammad Ahsan
Tera Contributor

there are three variables one is the date variable, in which I want that the user cannot select the date 9 days later from the current date, the other scenario is there are 2 date and time variable : booking from and booking till, I want that the user cannot select booking till time more than 3 hours based on booking from.

3 REPLIES 3

brianrichards
Tera Guru

The general approach to doing this will require stopping the user from submitting the form and then clearing the field value if it does not match your validation rules. It's not the same as preventing the value from being selectable in the first place, but it is the common convention for data/time field validation.

 

The technical tools you would use for this would be an onChange client script and a client-callable Script Include. This is necessary because all the good date/time APIs are server side, and doing anything with comparing date values on the client side tend to be problematic.

 

So the steps I would suggest are:

  • Create a Script Include with functions for both validation rules (and this could probably be a broadly used generic set of DateTime functions you might use for other kinds of validation too.
  • In the Script Include one function that tests based on days (using # of days as a parameter to make it flexible) and another one based on hours.
  • Create a Client Script set to execute On Change and pointing to the End Date part of the date field pair of fields you are checking.
  • (In the event the user sets the end date first and the start date second for some reason) create a second Client Script set to execute On Change and pointing to the Start Date.

 

For the use case where you want to check to make sure the date is not more than 9 days from the first field:

 

Catalog Client Script

Name: Check Dates on End Date Change

Type: onChange

Field: End Date (or whatever one matches the scenario you are testing)

function onChange(control, oldValue, newValue, isLoading) {

    var maxDaysBetween = 9;
    if (newValue) {
        var ga = new GlideAjax('yourlib_dateTimeUtilsAjax');
        ga.addParam('sysparm_name', 'isMoreThanDifferenceByDays');
        ga.addParam('sysparm_targetStartDate', g_form.getValue('start_date'));
        ga.addParam('sysparm_targetEndDate', g_form.getValue('end_date'));
        ga.addParam('sysparm_targetDifference', maxDaysBetween);
        ga.getXML(CheckEndDate);
    }
}

function CheckEndDate(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (answer == 'false') {
        return;
    }
    if (g_form.getValue('end_date') != '' && answer == 'true') {
        alert(getMessage("The End Date cannot be more than 9 days in the future"));
        g_form.setValue('end_date', '');
    }
}

 

Script Include

Name: yourlib_dateTimeUtilsAjax (must match the Catalog Client Script)

Client Callable: true

var yourlib__dateTimeUtilsAjax = Class.create();
yourlib__dateTimeUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    isMoreThanDifferenceByDays: function() {
        try {
            var targetStart = this.getParameter('sysparm_targetStartDate');
            var targetEnd = this.getParameter('sysparm_targetEndDate');
            var targetDifference = this.getParameter('sysparm_targetDifference');
            var testEndGDT = new GlideDateTime(targetStart);
            testEndGDT.addDaysLocalTime(targetDifference);
            var actualEndGDT = new GlideDateTime(targetEnd);
            if (actualEndGDT > testEndGDT) {
                return true;
            } else {
                return false;
            }

        } catch (err) {
            gs.error('Error in Script Include yourlib_dateTimeUtilsAjax: ' + err);
        }
    },

    isMoreThanDifferenceByHours: function() {
        try {
            var targetStart = this.getParameter('sysparm_targetStartDate');
            var targetEnd = this.getParameter('sysparm_targetEndDate');
            var targetDifference = parseInt(this.getParameter(sysparm_targetDifference));
            var targetDifferenceSeconds = (targetDifference * 3600);
            var testEndGDT = new GlideDateTime(targetStart);
            testEndGDT.addSeconds(targetDifferenceSeconds);
            var actualEndGDT = new GlideDateTime(targetEnd);
            if (actualEndGDT > testEndGDT) {
                return true;
            } else {
                return false;
            }
        } catch (err) {
            gs.error('Error in Script Include yourlib_dateTimeUtilsAjax: ' + err);
        }
    },

    type: 'yourlib_dateTimeUtilsAjax'
});

 

Notice that the Script Include has a second function that takes hours as a parameter and checks the difference based on this. Both of these functions work with negative numbers by the way, but that should not matter since the return value is either true or false.

 

This whole thing could probably use a wee bit of refinement, but it should get you close.

I just realized that the validation for your 9 day rule I used was based on there being both a start and end date and we would check between. Your requirements are different though, so I would use the following function in the Script Include instead:

 

    isMoreThanGivenDays: function() {
        try {
            var targetEnd = this.getParameter('sysparm_targetEndDate');
            var targetDifference = this.getParameter('sysparm_targetDifference');
            var testEndGDT = new GlideDateTime();
            testEndGDT.addDaysLocalTime(targetDifference);
            var actualEndGDT = new GlideDateTime(targetEnd);
            if (actualEndGDT > testEndGDT) {
                return true;
            } else {
                return false;
            }

        } catch (err) {
            gs.error('Error in Script Include bdr_dateTimeUtilsAjax: ' + err);
        }
    },

MuhammadAhsan_0-1685891657671.png

Hey there, as you can see there is booking date variable I used catalog UI policy to validate that the user can not select 9 days later date from today and it is working fine, but the issue is that you can see there are 2 more variables start time and end time, I want to apply validation that if user select today's date the user cannot select start time back from the current time, and also the maximum diff of start and end time must be 3 hours how can I resolve it, cannot reach to a solution