date time validation

lata1
Tera Contributor

I have a custom table in that i have 2 fields start date(date field) end date(date &time)

when user select start date end date should not allow to select after 7 days from start date( ex: start date is jan 1st user can able to select after 7th days only before that he cant select date and thrown error msg stating that please select after 7 days from start date)

can anyone help me onthis?

 

Thanks in advance

1 ACCEPTED SOLUTION

Chetan Mahajan
Kilo Sage
Kilo Sage

@lata1 ,

If you use UI Policy -> with condition Start date is not Empty && End Date - Less than - 7 - days - after -Start date and script as below 

function onCondition() {
    g_form.addErrorMessage('End Date should be 7 days after start date');
    g_form.clearValue('u_end_date');
}

Problem  : g_form.showFieldMsg may not be work and error message remains on form. 

 

If you use Onchange() Client script you can show g_form.showFieldMsg under field which looks good. try below code

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    var start = new Date(g_form.getValue('u_start_date'));
    var end = new Date(g_form.getValue('u_end_date'));
    var sevenDaysLater = new Date(start.getTime() + 7 * 24 * 60 * 60 * 1000);
    if (end < sevenDaysLater) {
		g_form.showFieldMsg('u_end_date','End date should be after 7 days of start date','error',false);
        g_form.setValue('u_end_date', '');
    }

}

you can decide which one is best according to you requirement.

 

Kindly mark my response correct and helpful if applicable. 

View solution in original post

4 REPLIES 4

Brad Bowman
Kilo Patron
Kilo Patron

I would suggest trying the no-code approach first to see if this works in your scenario

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-... 

Basheer
Mega Sage

Hi @lata1 ,

There are 2 ways of doing it.

1st Via UI Policy

Make your condition as

End Date is less than 7 days after start date

In the script you can directly enter the errormessage through g_form.ShowErrorMessage() and clear the end date through g_form.clearValue("end_date");

 

2nd is via On Change client script and Script Include

In Client Script

var start = g_form.getValue("u_start_date");
var end = g_form.getValue("u_end_date");
var startDate=new Date(start);
var endDate=new Date(end);

var diff=startDate.getTime()-endDate.getTime();
alert(diff);
var dayDiff = diff/ (1000 * 60 * 60 * 24);

if((startDate<endDate) && (dayDiff<(-7)))
		{
		alert("You have to enter end date till 7 days from today");
		g_form.setValue('u_end_date','');
		}
	

If the above solutions doesn't work then you need to write Client Script + Script Include.

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

Chetan Mahajan
Kilo Sage
Kilo Sage

@lata1 ,

If you use UI Policy -> with condition Start date is not Empty && End Date - Less than - 7 - days - after -Start date and script as below 

function onCondition() {
    g_form.addErrorMessage('End Date should be 7 days after start date');
    g_form.clearValue('u_end_date');
}

Problem  : g_form.showFieldMsg may not be work and error message remains on form. 

 

If you use Onchange() Client script you can show g_form.showFieldMsg under field which looks good. try below code

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
    var start = new Date(g_form.getValue('u_start_date'));
    var end = new Date(g_form.getValue('u_end_date'));
    var sevenDaysLater = new Date(start.getTime() + 7 * 24 * 60 * 60 * 1000);
    if (end < sevenDaysLater) {
		g_form.showFieldMsg('u_end_date','End date should be after 7 days of start date','error',false);
        g_form.setValue('u_end_date', '');
    }

}

you can decide which one is best according to you requirement.

 

Kindly mark my response correct and helpful if applicable. 

Hi Chetan,

 

On change script is working fine.

 

Thanks.