Date validation on a variable

Kanika4
Tera Contributor

Hi all

 

there is a variable on the form which should allow the date selection between 5 day prior and 2 weeks ahead from now.

how can i do that ?

5 REPLIES 5

SN_Learn
Kilo Patron
Kilo Patron

Hi @Kanika4 ,

 

Please try with the Catalog UI policies which requires no coding as instructed below:

 

No Code date validations through (Catalog) UI Policies 

 

Mark this as Helpful / Accept the Solution if this helps

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

Hi @SN_Learn,

 

Thank you for letting know about this article 😊. Haven't known we could achieve this with UI Policies, will try to implement this in future date validations.

Tried this for the above requirement and it worked.

 

@Kanika4 - Make sure to uncheck the On load checkbox. And write the clear value line of code prior, if you are using show field message method.

 


Mark the response correct and helpful if the answer assisted your question.

Rupanjani
Giga Guru

Hi @Kanika4,

 

If you want to display an error message as soon as the date is selected on the variable. You need to write an onChange Client Script and Script Include for validation.

 

Please find the below scripts:

OnChange Client Script: (on change of the field/variable - date field)

 

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

    var gaChng = new GlideAjax('Test'); //script include name
    gaChng.addParam('sysparm_name', 'dateValidation'); //second parameter - function name in the SI
    gaChng.addParam('sysparm_date', newValue);
    gaChng.getXML(getData1);

    function getData1(response) {
        var answer1 = response.responseXML.documentElement.getAttribute('answer');
		
        if (answer1 == 'false') {
            g_form.clearValue('<field_name>'); //replace <field_name> with the field/variable name
            g_form.showFieldMsg('<field_name>', '<add your error msg here>', 'error'); //replace <field_name> with the field/variable name
        }
    }
}

 

 

Script Include: (make client callable checkbox : true)

 

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

    dateValidation: function() {
        try {
            var selected_date = new GlideDateTime(this.getParameter('sysparm_date'));
            var flag = true;
            selected_date = selected_date.getDate();

            var gdt1 = new GlideDate();
            gdt1.addDays(14);

            var gdt2 = new GlideDate();
            gdt2.addDays(-5);

            if (selected_date.after(gdt1) || selected_date.before(gdt2)) {
                flag = false;
            }
            return flag;
        } catch (ex) {
            gs.info('Error:' + ex.string() + '\nLine:' + ex.lineNumber);
        }
    },

    type: 'Test'
});

 

 

Make sure to change the Script Include name, function name and field/variable names in Client script.

 


Mark the response correct and helpful if the answer assisted your question.

Hi This is not working, The only dates I can select after implementing this code is 07/07/2024 and 22/07/2024.

It throws errors for all the dates which are 5 days prior today (03/07/2024...... 06/07/2024) and also cant select any date after 22/07/2024

 

dateValidation: function() {
        try {
            var selected_date = new GlideDateTime(this.getParameter('sysparm_date'));
            var flag = true;
            selected_date = selected_date.getDate();

            var gdt1 = new GlideDate();
            gdt1.addDays(14); //22 july

            var gdt2 = new GlideDate();
            gdt2.addDays(-5);  //03 july

            if (selected_date.after(gdt1) || selected_date.before(gdt2)) {
                flag = false;
            }
            return flag;
        } catch (e) {
            gs.info('Error SI: Utils : dateValidation' + e + e);
        }
    },
 
 
 
 
Client script
 
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    var gaChng = new GlideAjax('RNCRUtils');
    gaChng.addParam('sysparm_name', 'dateValidation');
    gaChng.addParam('sysparm_date', newValue);
    gaChng.getXML(getData1);

    function getData1(response) {
        var answer1 = response.responseXML.documentElement.getAttribute('answer');
       
        if (answer1 == 'false') {
            g_form.clearValue('u_last_day');
            g_form.showFieldMsg('u_last_day', 'The last day can be only 5 days prior and 2 weeks ahead from now.', 'error');
        }
    }
}