Start date and end date validation

aamir1
Mega Expert

Hi,

I am new to service-now and facing some issues.

I have two date field variables in my catalog item and i need to do some validations. I tried but not able to achieve this.

My requirement is -

a) Start date should be less than the end date and start date should not be less than current date. If it happens then throw error.

b) End date should not be less than current date and start date, If it happens then throw error.

Please help with examples.

Thanks

1 ACCEPTED SOLUTION

shruti_tyagi
ServiceNow Employee
ServiceNow Employee

Ok try this simple script:



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


    if (isLoading || newValue === '') {


          return;


    }




      var startDate = g_form.getValue("start_date");


      var endDate = g_form.getValue("end_date");


      var format = g_user_date_time_format;



      var startDateMs = getDateFromFormat(startDate, format);


var endDateMs = getDateFromFormat(endDate, format);


//alert("Hi "+startDate);


//alert("end" + endDate);


if (startDateMs > endDateMs ) {


              g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("end_date"), g_form.getLabelOf("start_date")));




              return false;




      }


}



You should run this script on end date check this screenshot:


Screen Shot 2017-10-11 at 11.51.46 PM.png



Thanks


Shruti


If the reply was informational, please like, mark as helpful or mark as correct!


View solution in original post

8 REPLIES 8

shruti_tyagi
ServiceNow Employee
ServiceNow Employee

Ok try this simple script:



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


    if (isLoading || newValue === '') {


          return;


    }




      var startDate = g_form.getValue("start_date");


      var endDate = g_form.getValue("end_date");


      var format = g_user_date_time_format;



      var startDateMs = getDateFromFormat(startDate, format);


var endDateMs = getDateFromFormat(endDate, format);


//alert("Hi "+startDate);


//alert("end" + endDate);


if (startDateMs > endDateMs ) {


              g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("end_date"), g_form.getLabelOf("start_date")));




              return false;




      }


}



You should run this script on end date check this screenshot:


Screen Shot 2017-10-11 at 11.51.46 PM.png



Thanks


Shruti


If the reply was informational, please like, mark as helpful or mark as correct!


Hi Shruti,



Original script is working. Sorry i was trying the code on start date variable. But now its working. But the problem is it is not capturing the other requirement.


Start date should not be less than the current date.


I created and another script for start date variable and put this code-


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


    if (isLoading || newValue == '') {


          return;


    }




  var startDate = g_form.getValue("newuser_startdate");


      var format = g_user_date_format;



      if (startDate === "")


              return true;



      // get date strings into a number of milliseconds since 1970-01-01


      var startDateMs = getDateFromFormat(startDate, format);


//alert("Hi "+startDateMs);


// 0 from "getDateFormat" means an invalid date string was passed to it


              if (startDate === 0)


                      g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("newuser_startdate")));



              return false;


   


}



But its not working.Please help


shruti_tyagi
ServiceNow Employee
ServiceNow Employee

In that case try this second simple script I pasted it seems to be working, I tested on change request form.



Thanks


Shruti


If the reply was informational, please like, mark as helpful or mark as correct!


hi shruti,


i tried your code but it was not working,it is accepting the previous dates also...please help me...i'm new to servicenow...i'm trying to do date validations using client script, awaiting for your reply




Thanks


Deepu