Help to validate date/time field

Flavio8
Kilo Contributor

Hello everyone,

I'm working on script to validate date/time field, which one the user shouldn't be able to enter a past date/time.

// Client script:
//Type: OnChange
//UI Type: All

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

    var gaDate = new GlideAjax('validateDateFieldsUtils');
    gaDate.addParam('sysparm_name', 'checkDateCurrentFuture');
    gaDate.addParam('sysparm_date', newValue);
    gaDate.getXML(validateDateResponse);


    function validateDateResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
		alert(answer);

        if (answer == 'false') {
            g_form.setValue('u_date', '');
            g_form.showFieldMsg('u_date', 'he date/time you entered is an invalid format.', 'error');
        }

    }

}


//Script include:
// Client callable: check


var validateDateFieldsUtils = Class.create();
validateDateFieldsUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	checkDateCurrentFuture: function(){
		
		var answer = "";
		
		var selectedDateTime = new GlideDateTime(this.getParameter('sysparm_date'));
		var selectedDate = selectedDateTime.getDate();
		
		gs.info('SelectedDate: ' + selectedDate);
		
		
		var currentDate = gs.nowDateTime();
		var nowDateTime = new GlideDateTime(currentDate);
		nowDateTime.setDisplayValue(currentDate, "dd/MM/yyyy hh:mm:ss");
		var nowDate = nowDateTime.getDate();
		
		gs.info('NowDate: ' + nowDate);
		
		
		if((selectedDate < nowDate) || (selectedDate == null)){
			answer = false;
		}else{
			answer = true;
		}
		
		return answer;
		
		
	},
	
    type: 'validateDateFieldsUtils'
});

 

The script is working fine. The only problem is that when I select the first day of every month, the return is false. I'll leave some screenshoots below.

find_real_file.png

 

find_real_file.png

 

Is this a bug or is it something with my script?

 

Thanks in advance.

 

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

4 REPLIES 4

Frank Tate
Giga Guru
Giga Guru

Can you show us what's in the log for the value of selectedDate and nowDate from the script include? Also, the script include can be simplified a bit using GlideDate() like so:

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

    checkDateCurrentFuture: function () {

        var answer = "";

        var selectedDateTime = new GlideDateTime(this.getParameter('sysparm_date'));
        var selectedDate = selectedDateTime.getDate();

        gs.info('SelectedDate: ' + selectedDate);

        var nowDate = new GlideDate();

        gs.info('NowDate: ' + nowDate);


        if ((selectedDate < nowDate) || (selectedDate == null)) {
            answer = false;
        } else {
            answer = true;
        }

        return answer;


    },

    type: 'validateDateFieldsUtils'
});

 

Apparently, gs.nowDateTime() uses the system time zone, which may be messing with your expected results: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0594666

Frank

Hi Frank,

Here's my log of when I selected day 1. What you said about the time zone makes sense, but I found it odd because it's working fine for the other days.

 

find_real_file.png

NowDate: 2022-01-06 (yyyy/MM/dd)

SelectedDate: 2022-01-02 (yyyy/dd/MM).

 

That's could be the problem. Is there any way to fix this?

 

Thanks!

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Thanks for your article, Mark! It worked very well to solve my problem.

Thanks again,

 

Flavio.