Client script date validation

Community Alums
Not applicable

 

Hello ,

 

Am having a below requirement , need to achieve this by using client script  ( we need to use this validation for one catalog)

 

Data ranges should be two fields From Date / To date - full month date pickers (can't do just YY/MM).
Create an on change client script for the start date.  Check the day and if it is not 1 then reset to 1.
Create an on change client script for end date. 
Check the day: i
1) if not 31 for month 01,03,05,07,08,10,12 - reset to 31 
2)if not 30 for 04,06,09,11. Reset to 30.
3) if month 02 check if year is one of:  2028, 2032, 2036, 2040, 2044, and 2048  then set day to 29 else set day to 28

 

Thanks,

Developer

4 REPLIES 4

Rob Bushrod
Tera Guru

Hi @Community Alums,

If you are overriding any choice the user makes by setting it back to first of month and last day of month, why are you not just offering them a dropdown with the months of the year in it for those fields. The user would then pick From Month May and To Date June, you could put some help around the fields so that the user is clear if it is May then they pick May for both fields. Equally you could set the choice to 1st Month Name and 28th/30th/31st month name to make it clear.

Please mark my answer correct and helpful if this works for you.

Many Thanks,

Rob

Community Alums
Not applicable

Hello @Rob Bushrod 

Thanks for your reply. We don't have option to provide dropdown choices there.

1) We have two fields start date and end date

2) For Start Date - If we select any date of current month -> Then we need to set it to 1
3) For End Date - If we select any date of current month -> Then we need to set it to last day of the month i.e 30 / 31

 

 

Hi @Community Alums,

Ok if you can't change the variables that are being used to ask the question then I guess a Catalog Client Script is your option. Have you written any script yet or are you just looking for advice? If you have written something please attach so I can have a look. 

Many Thanks,

Rob

Simon Christens
Kilo Sage

Hi,

Its not too difficult to make.

 

I did create 1 client script for each of the variables and 1 script include that can handle the date validation through GlideDateTime

 

Onchange on the start date variable:
BE AWARE THAT THE SYSPARM_FIELD Parameter is "start" <--- IMPORTANT

 

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

	var start = new GlideAjax('TimeTester'); //Script include name what is client callable
	start.addParam('sysparm_name', 'validateDate'); //Name of the Methos that validates the dates
	start.addParam('sysparm_field', 'start'); //Identify the field that triggers the validation
	start.addParam('sysparm_date', newValue); //Date
	start.addParam('sysparm_format', g_user_date_format); //The Date format of the current user
	start.getXMLAnswer(function(answer){
		//Check if the fields needs to be updated
		if(newValue != answer){

			g_form.setValue('start_date', answer);
		}
	});

}

 

 

And the End date client script:

BE AWARE IF THE SYSPARM_FIELD variable - ITS IMPORTANT

 

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

	var start = new GlideAjax('TimeTester'); //Script include name what is client callable
	start.addParam('sysparm_name', 'validateDate'); //Name of the Methos that validates the dates
	start.addParam('sysparm_field', 'end'); //Identify the field that triggers the validation
	start.addParam('sysparm_date', newValue); //Date
	start.addParam('sysparm_format', g_user_date_format); //The Date format of the current user
	start.getXMLAnswer(function(answer){
		//Check if the fields needs to be updated
		if(newValue != answer){

			g_form.setValue('end_date', answer);
		}
	});

}

 

 

The script include which are client callable:

 

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

    /*_________________________________________________________________
       * Description: Parses a date and validate if its 1st of last day of month
       * Parameters: dateField, date and date format - all strings 
       * Returns: Date with 1st or last day of month
       ________________________________________________________________*/
    validateDate: function() {

		var dateField = this.getParameter('sysparm_field'); //start or end
		var date = this.getParameter('sysparm_date'); //Date
		var format = this.getParameter('sysparm_format'); //Format

		var gDate = new GlideDateTime();
		gDate.setValueUTC(date, format); //Parses the date into the GDT with correct format

		var daysInMonth = gDate.getDaysInMonthUTC(); //Returns amount of days in the current month
		var dayOfMonth = gDate.getDayOfMonthUTC(); //Return the current date / day of the month

		//If its the start date we validate against
		if(dateField == 'start' && dayOfMonth != 1){
			//Set the date to the 1st of the month in case its not already the 1st
			gDate.setDayOfMonthUTC(1);

		//If end date
		}else if(dateField == 'end' && dayOfMonth != daysInMonth){

			//Sets the date to the last day of the month in case its not already
			gDate.setDayOfMonthUTC(daysInMonth);
		}
		
		//Returns the display value because the variables need that
		return gDate.getDate().getDisplayValue();
    },

    type: 'TimeTester'
});

 

 

This should work