Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Date/Time validation for Date/Time variable

Kifrez
Kilo Guru

I have 2 variables which are start_date_time and end_date_time for service catalogue

Both variables have Date/Time type. FYI, I am in Australia.

 

My script won't work for the following scenario. Could you please help?

- if start_date_time is less than a minute ago, clear value of end_date_time.

- if the start_date_time is today and in the past, clear value of end_date_time

- if the end_date_time is older than start_date_time, clear start_date_time

 

 

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

    var startDateTime = g_form.getValue('start_date_time');
    var endDateTime = g_form.getValue('end_date_time');
    var currentDateTime = new GlideDateTime();

    // Check if the start_date_time is in the past
    if (startDateTime < currentDateTime) {
        g_form.clearValue('start_date_time');
        alert("Start date and time cannot be in the past.");
        return;
    }

    // Check if the start_date_time is today and in the past
    if (startDateTime.equals(currentDateTime) && g_form.getValue('start_date_time') <= currentDateTime) {
        g_form.clearValue('start_date_time');
        alert("Start time cannot be the current time or in the past for today.");
        return;
    }

    // Check if end_date_time is older than start_date_time
    if (endDateTime < startDateTime) {
        g_form.clearValue('start_date_time');
        alert("Start date and time cannot be later than the end date and time.");
        return;
    }
}

 

4 REPLIES 4

Danish Bhairag2
Tera Sage

Hi @Kifrez ,

 

U can use UI policy here instead of client script & write the script part for clearance & alert when condition matches to true.

 

Thanks,

Danish

 

Harsh_Deep
Giga Sage

Hello @Kifrez ,

 

Remove last three return; from your code.

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

    var startDateTime = g_form.getValue('start_date_time');
    var endDateTime = g_form.getValue('end_date_time');
    var currentDateTime = new GlideDateTime();

    // Check if the start_date_time is in the past
    if (startDateTime < currentDateTime) {
        g_form.clearValue('start_date_time');
        alert("Start date and time cannot be in the past.");
    }

    // Check if the start_date_time is today and in the past
    if (startDateTime.equals(currentDateTime) && g_form.getValue('start_date_time') <= currentDateTime) {
        g_form.clearValue('start_date_time');
        alert("Start time cannot be the current time or in the past for today.");
    }

    // Check if end_date_time is older than start_date_time
    if (endDateTime < startDateTime) {
        g_form.clearValue('start_date_time');
        alert("Start date and time cannot be later than the end date and time.");
    }
}

 

Please mark my answer as helpful and solution accepted.
Thanks

Anand Kumar P
Tera Patron

Hi @Kifrez ,
Check if start_date_time is in the past or less than a minute ago.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
    var startDateTime = g_form.getValue('start_date_time');
    var endDateTime = g_form.getValue('end_date_time');
    var currentDateTime = new GlideDateTime();

    // Check if start_date_time is in the past or less than a minute ago
    var oneMinuteAgo = new GlideDateTime();
    oneMinuteAgo.addSeconds(-60);

    if (startDateTime < oneMinuteAgo) {
        g_form.clearValue('end_date_time');
        alert("Start date and time cannot be in the past or less than a minute ago.");
        return;
    }

    // Check if start_date_time is today and in the past
   if (startDateTime.equals(currentDateTime) && g_form.getValue('start_date_time') <= currentDateTime) {
        g_form.clearValue('start_date_time');
        alert("Start date and time cannot be in the past.");
        return;
    }

    // Check if end_date_time is older than start_date_time
    if (endDateTime < startDateTime) {
        g_form.clearValue('start_date_time');
        alert("Start date and time cannot be later than the end date and time.");
    }
}

Please mark it as solution proposed and helpful if it works for you.
Thanks,

Anand

Danish Bhairag2
Tera Sage

Hi @Kifrez ,

 

I missed the part that you are using GlideDateTime in client script which does not work over there. Kindly use GlideAjax to validate the condition & return the result as a response from there.

 

Thanks,

Danish