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

Anurag Tripathi
Mega Patron
Mega Patron

Hi Nat,



I had done something similar,



Here is how i did it, hope it helps



var msg;


var dateFormat=g_user_date_time_format;


var task_start = g_form.getValue('start_date');


var task_end = g_form.getValue('end_date');



var flag=compareDates(task_start , dateFormat , task_end   , dateFormat);


if( flag== 1)


  {


  msg = 'Start date cannot be more that End date. ';


  alert(msg);


}


-Anurag

Thanks - I've tried this but it doesn't show any alert (or error)??


its working ideally for me,



my fields are glide date time....


-Anurag

Hello Anurag,



I am getting error on below line.


var dateFormat=g_user_date_time_format;



Regards,


Chara


poyntzj
Kilo Sage

i have this as a starrt date validation routine


There is one for end date too



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


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


              return;


      }


   


      g_form.hideFieldMsg('start_date');


      var dtEndForm = g_form.getValue('end_date');


      var dtEnd = getDateFromFormat(dtEndForm,g_user_date_time_format);


   


      if (dtEndForm != '') {


              var dtStartForm = g_form.getValue('start_date');


              var dtStart = getDateFromFormat(dtStartForm,g_user_date_time_format);


              if (dtEnd < dtStart) {


                      g_form.showFieldMsg('start_date', 'Planned start date cannot be after end date', 'error');


              }


      }


   


}