Date Validaion

Atik
Tera Contributor

Hi,

I'm working on a form validation where there are two fields in a section both are 'date' fields 

field - 1 : u_test1

field - 2 : u_test2

 

u_test1 should not be greater than u_test2

 

u_test1 should not be less than today.

 

can anybody help me on this?

 

Thanks in advance!

 

4 REPLIES 4

Harish KM
Kilo Patron
Kilo Patron

Hi you can have 2 onChange Client scripts on test 1 and test 2 fields

 

Script 1 on test1 field to validate date cannot be today and past days

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


g_form.hideErrorBox('u_test_1');
var date = g_form.getValue('u_test_1');
date = new Date(Date.parse(date.replace(/-/g,' ')));
var today = new Date();
today.setHours(0,0,0,0);
if (date <= today) {
g_form.showErrorBox('u_test_1',getMessage('Test1 date cannot be todays date'));

}


}

 

Script 2 on test 2 field to validate cannot be before test 1 date

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

g_form.hideErrorBox('u_test_2');
var start_date = g_form.getValue('u_test_1');

if (start_date) {
var startMS = getDateFromFormat(start_date, g_user_date_format);
var endMS = getDateFromFormat(newValue, g_user_date_format);

if (endMS < startMS)
g_form.showErrorBox('u_test_2',getMessage('Test 2 date must be after test 1 date'));
}


}

Regards
Harish

praneeth7
Tera Guru

Hello @Atik 

You can write a before insert BR using this script,

        var date1 = current.u_test1;
	var date2 = current.u_test2;
	var today = new GlideDate();

	if (date1>date2 || date1<today){
		gs.addErrorMessage('u_test1 should not be grater than u_test2 and u_test1 should not less than today');
		current.setAbortAction(true);
	}

Please mark my answer Helpful and accepted solution,

Thank you

Nitesh A
Tera Expert

Hello @Atik 

Can you try using below condition in UI policy

NiteshAlashe_0-1690273513795.png

Please mark this response as correct or helpful if it assisted you with your question.

Regards,
Nitesh