Begin Date to End Date duration cannot exceed one year

Cupcake
Mega Guru

I have a need to make sure that the duration between the Begin Date and End Date does not exceed 1 year.

I used scripting before to make a date go out to xxx amount of days, but never to actually do a calculation between Begin and End dates

I don't have a Due Date field. I just have the Start and End Date field and I need to make sure that Duration cannot exceed 1 year.

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Hello Karen, its been a while hope you are well!



Here is some example code I have used before.   Do you want the validation to happen on submit or as the user is inputting the data?   My code below is based on an onChange of the Work End date on a task, but you could change it to an onSubmit can calculate and warn then.



Create a onChange client script for the End date:


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


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


              return;


      }



      var starts = g_form.getValue('work_start');


      startMillis = getDateFromFormat(starts, g_user_date_time_format);


     


      var ends = newValue;


      endMillis = getDateFromFormat(ends, g_user_date_time_format);


     


      var minutes = (endMillis - startMillis) / (1000 * 60);


     


      if(minutes > 480) {


              alert('An Activity may only last 8 hours.   You must enter multiple activities if the time required exceeds 8 hours.');


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


      }


}



Obviously you will need to calculate the a year to then compare for your alert.   Thinking out loud an onSubmit may be better because you can prevent the save if it exceeds a year.


View solution in original post

27 REPLIES 27

The script that I provided earlier was sample script.


Here is my script directly. and the field that I want to validate is the insinq inquiry number field. It must be exactly 11 character, not less than 11 - not more than 11 and it must be all numeric.



function onSubmit() {



  //Type appropriate comment here, and begin script below



  //Require at least 10 characters in the 'Comments' field



  var maxLength = 11;



  var control = g_form.getControl('scan_insinq_inquiry_num');



  if(control.value.length < maxLength){



  g_form.hideErrorBox('scan_insinq_inquiry_num');



  g_form.showErrorBox('scan_insinq_inquiry_num', 'You must type exactly ' + maxLength + ' characters in this field. You have entered characters less than ' + maxLength);



  return false;



  }



  else if(control.value.length > maxLength){



  g_form.hideErrorBox('scan_insinq_inquiry_num');



  g_form.showErrorBox('scan_insinq_inquiry_num', 'You must type exactly ' + maxLength + ' characters in this field. You have entered characters more than ' + maxLength);



  return false;



  }



  else{



  g_form.hideErrorBox('scan_insinq_inquiry_num');



  }



}


Hi Abhinay,


        I used the onChange Script below which solved my problem



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


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


                  return;


        }



      var num = new RegExp("^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$");


        if(!num.test(newValue)){


                  alert('Please enter numbers only in this field');


                  control.value = oldValue;


        }



}



The onSubmit worked too, I just couldn't figure out how to incorporate that the characters had to be numeric only. So both solutions are acceptable.



Thanks,


Karen


Here you go. Try this. Moreover you do not need such a long regular expression. You can just use \D to check for non-digit characters.


function onSubmit() {



  //Type appropriate comment here, and begin script below



  //Require at least 10 characters in the 'Comments' field



  var maxLength = 11;



  var control = g_form.getControl('scan_insinq_inquiry_num');


  if(/\D/g.test(control.value)){


  //put your g_form.showErrorBox code here


  }


  else{


  if(control.value.length < maxLength){



  g_form.hideErrorBox('scan_insinq_inquiry_num');



  g_form.showErrorBox('scan_insinq_inquiry_num', 'You must type exactly ' + maxLength + ' characters in this field. You have entered characters less than ' + maxLength);



  return false;



  }



  else if(control.value.length > maxLength){



  g_form.hideErrorBox('scan_insinq_inquiry_num');



  g_form.showErrorBox('scan_insinq_inquiry_num', 'You must type exactly ' + maxLength + ' characters in this field. You have entered characters more than ' + maxLength);



  return false;



  }



  else{



  g_form.hideErrorBox('scan_insinq_inquiry_num');



  }


  }



}