How to Restrict past date in Date field - Onchange client script

chanikya
Tera Guru

Hi,

i have BR script , it is working fine , there is no problem with BR script, i would like to see this functionality in client script

CLIENT SCRIPT -ONCHANGE 

BR script:  how can i write it into Client Script

var today= new GlideDateTime();
var past=new GlideDateTime(current.u_review_date);
var todaydate=today.getLocalDate();
var pastdate=past.getLocalDate();
if(pastdate.before(todaydate))
{
gs.addInfoMessage("Please don't select past date");
current.u_sunset_review_date="NULL";
current.setAbortAction(true);
}

1 ACCEPTED SOLUTION

Shashikant Yada
Tera Guru

You can follow below link, using Script Include and Client script will resolve your issue.

https://community.servicenow.com/community?id=community_question&sys_id=abd88f61db5cdbc01dcaf3231f9619f5

Thanks
Shashikant

Hit Helpful or Correct on the impact of response.

View solution in original post

21 REPLIES 21

Daniele Songini
Tera Guru

Hi,

the APIs GlideDate and GlideDateTime are usable only on server side.

I suggest you create an script include and checks and retrieves the value using a GlideAjax.

Please consider that creating a function in the script includes ( isInThePast( date ) ), then you can recall it in every script, both client and server, making the code easier to maintain.

Try also to look at this.
Client Side Dates in ServiceNow

Please mark this as "Correct Answer" if I have given you a sufficient answer to your question.

Best Regards,
Daniele

Wirasat
Tera Guru

You can use following script in onChange client script for date validation. No need to call script include.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

if (isLoading || newValue === '') {
return;
}

var now = new Date();
now = formatDate(now, g_user_date_time_format);
now = getDateFromFormat(now, g_user_date_time_format);
var X = g_form.getValue('review_date'); //Review date is field on the form.

X = getDateFromFormat(X, g_user_date_time_format);
if (X < now) {
alert('Selected date is in the past.');
g_form.setValue('review_date', '');
}

return false;
}

Hi,

It is working fine , successfully restricting past date and as well is it possible to allow today date too..

 

because it is restricting Today date, 

it should allow today date and Future dates.....

 

find_real_file.png

 

find_real_file.png

 

find_real_file.png

Hi,

That is because it even count seconds also. I think you are selecting today date by clicking 'Go to today'. Once you select the today date just make 1 min after manually and try to submit form. In below script added alert. You can compare now and selected time. your selected datetime should be after now

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}
	
	
	var now = new Date();
	now = formatDate(now, g_user_date_time_format);
	
	alert("now.."+now+" "+"selected date.."+newValue);
	now = getDateFromFormat(now, g_user_date_time_format);
	
	var x = newValue;
	
	x= getDateFromFormat(x, g_user_date_time_format);
	
	if (x<now) {
		alert('Selected date is in the past.');
		g_form.setValue('review_date', '');
		return false;
	}
	
	
}