Users should not be able to enter a date that is more than one year in the future.

kevin246her
Giga Contributor

Hi,

 

I need to restrict users from entering a date more than one year in advance. If they do, an error message should be displayed, and they should not be able to save the form. I've tried creating an on-submit client script, but it's not working. Can anyone suggest a solution?

 

Thanks in advance.

5 REPLIES 5

Mark Manders
Mega Patron

Create an onChange client script, showing a message and emptying the field. That way the form can't be saved, since the field is mandatory (I assume, since you don't want the form saved without a date).

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var currentDate = new Date();
    var inputDate = new Date(newValue);

    // Calculate the difference in milliseconds
    var differenceInMillis = inputDate - currentDate;

    // Convert milliseconds to days
    var differenceInDays = differenceInMillis / (1000 * 60 * 60 * 24);

    // Check if the difference is more than 365 days (1 year)
    if (differenceInDays > 365) {
        g_form.showErrorBox('your_date_field', 'Date cannot be more than 1 year in the future.');
        g_form.setValue('your_date_field', '');
    } else {
        g_form.hideErrorBox('your_date_field');
    }
}

Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

@Mark Manders,

This is not advisable because we have API's to addYears with GlideDateTime, so introducing maths does not factor in timezone and daylightsaving periods, also your messages are hard-coded which is an internationalization infraction called a "hard-coded string" (meaning it is never translatable),

Many thanks,
Kind regards

--------------------------------------------------------------------
Director of Globalization Deployment, Internationalization

Daylight saving is a difference of an hour, how does that relate do a date field? And timezones can be ignored when you are checking on a date to see if it is a YEAR away.

Next to that: a solution was asked, without mentioning it should be translated or anything like that. Of course you don't want hard coded messages when you have multiple languages on an instance, but where was this mentioned to be a requirement? You are making assumptions that aren't based on anything. A solution was asked and based on the given information a possible solution was given. These can (and should) be tweaked to get the perfect solution, but you are getting date/time involved and translations, that were nowhere mentioned.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Alex Coope - SN
ServiceNow Employee
ServiceNow Employee

Hi @kevin246her,

This isn't really a topic for internationalization, but I can tell you that you essentially would want a server-side script (because it's more efficient - so think a before BR with a potential abort action at the earliest order on the table and a redirect back to current),

In that script you would want two Date (or DateTime variables), one representing the field value as is, one representing today (using this + 1 year) to compare against.

Use the API details here to review how to construct the use of "AddYears" in the GlideDateTime API:
https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server_legacy/c_GlideDateTimeAP...


Many thanks,
kind regards

--------------------------------------------------------------------
Director of Globalization Deployment, Internationalization