How to validate date/time using client scripts and script include?

Shikha15
Tera Contributor

I'm working on a scoped application (which include service portal) for which one of the requirement is to validate date/time field. So, the user shouldn't be able to enter a future date/time. I did this using catalog ui policy so it would reflect in service portal. But it is still not working fine and our team was recommended to use client scripts and script include for doing the same. 

Since, glide properties won't work in service portal, we need to do this using JAVA AJAX. Please help on this.

 

Thanks in advance.

2 REPLIES 2

poojamehta237
Tera Contributor

Hey Sikha,

For Client Script, you can use onChange client script and can check on real time.

You can refer this code and can change as per your need

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
       return;
   }
   var date = new Date();
   alert(date);
   getFormattedDate(date);

   function getFormattedDate(date) {
       var year = date.getFullYear();
       var month = (1 + date.getMonth()).toString();
       month = month.length > 1 ? month : '0' + month;

       var day = date.getDate().toString();
       day = day.length > 1 ? day : '0' + day;

       var d = month + '-' + day + '-' + year;
g_form.setValue('real_date', d);
   

 

Thank You

If you find my suggestions helpful, please mark it as correct or helpful to help out others as well 🙂

mhedtke24
Tera Expert

Script include:

var ValidateDateFieldsUtils = Class.create();
ValidateDateFieldsUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	checkDateEntered: function() {
		
		// Check date to validate it is in the future
		
		var answer = "";
		
		// Date selected by User, date/time value, and only date value
		var selectedDateTime = new GlideDateTime(this.getParameter('sysparm_date'));
		var selectedDate = selectedDateTime.getDate();                   
		
		// Current date/time, date/time value, and only date value
		var nowDateTime = new GlideDateTime(gs.nowDateTime());
		var nowDate = nowDateTime.getDate();                             
		
		// if date entered is before the current date, is the current date, ir is empty, return false;
		if ((selectedDate <= nowDate) || (selectedDate == null)) {
			answer = false;
		} else {
			answer = true;
		}
		return answer;
	},
	
	checkDateEnteredPast: function() {
			
		// Check date to validate it is in the past
		
		var answer = "";
		
		// Date selected by User, date/time value, and only date value
		var selectedDateTime = new GlideDateTime(this.getParameter('sysparm_date'));
		var selectedDate = selectedDateTime.getDate();                   
		
		// Current date/time, date/time value, and only date value
		var nowDateTime = new GlideDateTime(gs.nowDateTime());
		var nowDate = nowDateTime.getDate();                             
		
		// if date entered is after the current date, is the current date, or is empty, return false
		if ((selectedDate >= nowDate) || (selectedDate == null)) {
			answer = false;
		} else {
			answer = true;
		}
		return answer;
	},
	
	checkDateCurrentFuture: function() {
			
		// Check date to validate it is the current date or in the future
		
		var answer = "";
		
		// Date selected by User, date/time value, and only date value
		var selectedDateTime = new GlideDateTime(this.getParameter('sysparm_date'));
		var selectedDate = selectedDateTime.getDate();                   
		
		// Current date/time, date/time value, and only date value
		var nowDateTime = new GlideDateTime(gs.nowDateTime());
		var nowDate = nowDateTime.getDate();                             
		
		// if date entered is before the current date or is empty, return false
		if ((selectedDate < nowDate) || (selectedDate == null)) {
			answer = false;
		} else {
			answer = true;
		}
		return answer;
	},
	
	type: 'ValidateDateFieldsUtils'
});

 

Client Script:

 
Type: onChange
UI Type: All
 
function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
// Hide any field level messages for Date field
	g_form.hideFieldMsg('<variable name here>');
	 
	// Call the script include to do the date check
	var ga = new GlideAjax('ValidateDateFieldsUtils');
	ga.addParam('sysparm_name','checkDateEnteredPast');
	ga.addParam('sysparm_date', newValue);
	ga.getXML(ValidateDateResponse);
}

function ValidateDateResponse(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	// If the date is less than the current date, display a field level error message
	if(answer == "false"){
		g_form.showFieldMsg('<variable name here>', "The date/time you entered is an invalid format, the current date or after the current date", 'error');
		g_form.setValue('<variable name here>', "");
	}
}