Client script to validate start/end date fields

nmcl
Giga Expert

We have 'end date validation' on our change request form, so that the end date has to be after the start date (logical).

I've copied this script over to a catalog client script but it's not working in the same way, it simply prompts regardless of the date entered.   Any ideas?

 

--- script ---

 

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

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

  return;

  }

 

  var end = g_form.getValue("loan_required_to");

  var start = g_form.getValue("loan_required_from");

  // skip if start or end is blank

  if (start == "" || end == "") {

  return

  }

 

  // get user's date time display format

  var format = g_user_date_time_format;

  var isEndBeforeStart = compareDates(start, format, end, format);

 

  // 1 (true) if start is greater than end

  // 0 if end is greater than start of if they are the same

  // -1 if either of the dates is in an invalid format

 

  if (isEndBeforeStart) {

  alert("End must be after start");

  }

}

1 ACCEPTED SOLUTION

nmcl
Giga Expert

Thanks all for your suggestions.   This was fixed by changing the field from date to date/time which then matches the format from the change form. The same code then works as oob.


View solution in original post

20 REPLIES 20

Harish Murikina
Tera Guru

Hi Nat,



                    Write this below script onchange of end date.


   


function onChange(control, oldValue, newValue, isLoading)


{


  if(isLoading)


  {


  return;


  }


   


  var ga = new GlideAjax('includescriptname');


  ga.addParam('sysparm_name','dateFunction');


ga.addParam('start_date', g_form.getValue('startdate'));


  ga.addParam('end_date', newValue);


  ga.getXML(HelloWorldParse);


   


  function HelloWorldParse(response)


  {


  var answer = response.responseXML.documentElement.getAttribute("answer");  


  if(answer>=0)


  {


  return true;


  }


  else


  {


  alert("please enter   end date after start date");


  g_form.setValue('Depart_On','');


  return false;


  }


   


  }


   


}





Writhe this code in includescript


_____________________________



dateFunction: function()


{


var sdate = this.getParameter('start_date');


var sdate = this.getParameter('end_date');


var currentdate=gs.now();


var answer = gs.dateDiff(sdate ,edate,true);


return answer;


},





Regards,


Harish.


Hi Harish, thanks for this. I'm just trying to implement it but not having much luck.


I've setup up the catalog client script based on your update (modified field names to suit)...



function onChange(control, oldValue, newValue, isLoading)
{
  if(isLoading)
  {
  return;
  }
  var ga = new GlideAjax('Laptop loan required to date validation');
  ga.addParam('sysparm_name','dateFunction');
  ga.addParam('start_date', g_form.getValue('loan_required_from'));
  ga.addParam('end_date', newValue);
  ga.getXML(HelloWorldParse);


  function HelloWorldParse(response)
  {
  var answer = response.responseXML.documentElement.getAttribute("answer");  
  if(answer>=0)
  {
  return true;
  }
  else
  {
  alert("The to date must be the same as or after the from date");
  g_form.setValue('loan_required_from','');
  return false;
  }
  }
}





I'm trying to create the script include but get the below errors...


Error MessageJavaScript parse error at line (8) column (2) problem = syntax error


WARNING at line 1: Label 'dateFunction' on function statement.

ERROR at line 1: Missing name in function declaration.


Hi Nat,



                  Try below code. And keep one gs.log in includescript.



function onChange(control, oldValue, newValue, isLoading)


{


  if(isLoading)


  {


  return;


  }


  var ga = new GlideAjax('Laptop loan required to date validation');


  ga.addParam('sysparm_name','dateFunction'); // This function name should be your include script function name


  ga.addParam('start_date', g_form.getValue('loan_required_from'));


  ga.addParam('end_date', newValue);


  ga.getXML(HelloWorldParse);


 


  }


 


function HelloWorldParse(response)


  {


  var answer = response.responseXML.documentElement.getAttribute("answer");  


  if(answer>=0)


  {


  return true;


  }


  else


  {


  alert("The to date must be the same as or after the from date");


  g_form.setValue('loan_required_from','');


  return false;


  }


  }



Regards,


Harish.


Thanks Harish, I don't understand the part re keeping one gs.log in includescript?