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

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.


From memory, you can then use g_user_date_format


vjay
Kilo Contributor

Hi Nat,



I'm new to SNow.



I used the same code and it is popping a message "End must be after start". So the code works fine.



Problem raises when i click the pop up OK button it is accepting and i can able to submit the form. But it should not allow the user to submit the form until start>end. Please suggest the condition so that it should not allow the user to submit if the condition fails.



Thanks,


Vjay


I add an error message to the date field on the form


divvi_vamsi
Mega Expert

Is compareDates(start, format, end, format); an OOB method..where can we find the implementation of it?



Thanks


Divya