4 Replies Latest reply on Feb 18, 2014 3:16 PM by berocca

    Specify a date range for a date field.

    berocca Level 1

      Hi folks,

       

      I have cobbled together some javascript using code that someone provided in response to my thread about setting a maximum length between two dates as reference.

       

      My aim is to enforce a minimum date of 01/01/2014 and a maximum date of 31/12/2014. Any date entered that are outside of tis range should result in a warning message.

       

      When I use the below code, I find that the warning message pops up before I ahve even had a chnace to enter any dates, and also when I enter an invalid date it pops up over and over until the program crashes.

       

      Can anyone tell me how I can fix this?

       

      Cheers.

       

      function Scand(cFormat, cString) {
      var oDate = util.scand(cFormat, cString);
      if(oDate == null) app.alert("Error converting date " + cString + " with format of " + cFormat, 0, 0);
      return oDate;
      }
      function GetField(cName) {
      var oField = this.getField(cName);
      if(oField == null) app.alert("Proposed Completion date cannot be more than 180 days after Commencement Date. Please revise " + cName);
      return oField;
      }

      var date1 = (GetField('Field 30')).value;

      var cDateFormat = 'dd-mm-yyyy'; // adjust to match input field's format;

      var oDate1 = Scand(cDateFormat, date1)

      var nDate1 = Math.floor(oDate1.getTime() / (1000 * 60 * 60 * 24))

      if(nDate1 < 01-01-2014) app.alert('Date cannot be earlier than 01/01/2014');

      if(nDate1 > 31-12-2014) app.alert('Date cannot be later than than 31/12/2014');

      this.getField("Proposed Completion Date").setFocus();
      event.rc = false;