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

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


that code will add a message below the relevant field


use this which uses your fields and will pop up an alert



function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
            return;
    }
   
    var dtLRTForm = g_form.getValue('loan_required_to');
    var dtLRT = getDateFromFormat(dtLRTForm,g_user_date_time_format);
   
    if (dtLRTForm != '') {
            var dtLRFForm = g_form.getValue('loan_required_from');
            var dtLRF = getDateFromFormat(dtLRFForm,g_user_date_time_format);
            if (dtLRT < dtLRF) {
                    alert("Required Until date must be after Required From date");
            }
    }
   
}

Thanks, I don't see the message though?


this worked for me try once



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


      var ds=getDateFromFormat(desStart , g_user_date_time_format);


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


      var de=getDateFromFormat(desComp , g_user_date_time_format);


      var sub=de-ds;


      if(sub<0)


              {


              g_form.hideErrorBox('end_date');


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


              g_form.showErrorBox('end_date', "Planned end date must be after Planned start date");


      }


      else


              {


              g_form.hideErrorBox('end_date');


              g_form.hideErrorBox('start_date');


      }


OK,


this is from our instance and this works when checking against the fields. the only difference is that we add the message to the field and not an alert, so I changed taht section


you will need to amend the getValue lines (10 and 13) to read your variables and update the alert on line 20



//This script caters for the END date being input or amended to be BEFORE the START date.



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


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


          return;


  }


   



   


var adateForm = g_form.getValue('variables.start_datetime');


var adate = getDateFromFormat(adateForm, g_user_date_time_format);



var aedateForm = g_form.getValue('variables.end_datetime');


var aedate = getDateFromFormat(aedateForm, g_user_date_time_format);



           



      if (adate != '' && aedate != '' && adate > aedate)


      {


      alert("Put your message here");


      }


           


}