4 Replies Latest reply on Jan 10, 2017 10:04 AM by -MaddAddaM-

    Date Validation Bug Assistance



      I've searched the forums "date validation" topics but haven't seen this particular issue discussed. Sorry in advance if it's redundant.


      I have two fields: "Month Ended" and Due_Date." Month Ended is user input with "date validation." Due_Date is calculated based on the user input in Month Ended.

      The format for Month Ended and Due_Date is set to validate for mm/dd/yyyy.


      Here are three scenarios and results:

      1. User enters 9/30/2016. Due Date updates to 10/31/2016.

      2. User enters 9/30/16. Validation script catches and prompts them to re-enter.

      3. User enters 9/30. Month Ended "auto-corrects" to 9/30/2016 on the screen. But then Due Date adjusts to 01/00/0000.


      Scenario 3 is what I need help resolving.


      Below is the code in each text box.

      MONTH ENDED DATE VALIDATION BOX (Note: This is also in the Date Validation box for Due_Date)


      // Validates that the input string is a valid date formatted as "mm/dd/yyyy"

      function isValidDate(dateString)


          //first check for the pattern


              return false;



          //parse the date parts to integers

          var parts = dateString.split("/");

          var day = parseInt(parts[1], 10);

          var month = parseInt(parts[0], 10);

          var year = parseInt(parts[2], 10);



          //check the ranges of month and year

          if(year < 1000 || year > 3000 || month == 0 || month > 12)

              return false;



          var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];



          //adjust for leap years

          if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))

              monthLength[1] = 29;



          //check the range of the day

          return day > 0 && day <= monthLength[month - 1];





      //set variables and populate

      var monthEnded = new Date(this.getField("Month Ended").value);

      var currentYear = monthEnded.getFullYear();

      var currentMonth = monthEnded.getMonth();

      var lastDayofNextMonth = new Date(currentYear, currentMonth+2, 0);

      event.value = util.printd("mm/dd/yyyy", lastDayofNextMonth);

        • 1. Re: Date Validation Bug Assistance
          gkaiseril MVP & Adobe Community Professional

          Acrobat JavaScript has a number of methods for dealing with dates that Web JavaScript does not. Have you checked to see if your statement


          var monthEnded = new Date(this.getField("Month Ended").value);


          actually creates a valid date object?


          You script corrected with some debugging informational information showing on the Acrobat JavaScript console:


          //set variables and populate



          console.println("\NInputted Month ;End string: " + this.getField("Month Ended").value)

          console.println("\Nnew Date(this.getField(\"Month Ended\").value): " + new Date(this.getField("Month Ended").value));

          console.println("\Nutil.scand(\"mm/dd/yyyy\", this.getField(\"Month Ended\").value));: " +util.scand("mm/dd/yyyy", this.getField("Month Ended").value));

          var monthEnded = util.scand("mm/dd/yyyy", this.getField("Month Ended").value);

          var currentYear = monthEnded.getFullYear();

          var currentMonth = monthEnded.getMonth();

          var lastDayofNextMonth = new Date(currentYear, currentMonth + 2, 0);

          console.println("\nLastDayofNextMonth: " + lastDayofNextMonth);

          event.value = util.printd("mm/dd/yyyy", lastDayofNextMonth);

          1 person found this helpful
          • 2. Re: Date Validation Bug Assistance
            gkaiseril MVP & Adobe Community Professional

            "/Your validation script will work only if the user enters the date with the "/" delimiter. Unfortunately or fortunately the acceptable separators for dates allow the ".". "-",  and " " (space") characters. The exact value of the field will be the user entered value and not the display format. You can modify your RegExp string to accept these other characters. But you might also want to consider using the "util.scand" method of Acrobat JavaScript that will check the entered date string as being a valid date or not. One can then use the various JavaScript date object methods to determine if the date entered is the last day of the month for the date entered.


            When using the custom validation you might want to include a message when the validation fails to explain the error and how to correct it. Also one can set the Acrobat JavaScript event return code property to single to the JavaScript processes that the validation failed.

            1 person found this helpful
            • 3. Re: Date Validation Bug Assistance
              -MaddAddaM- Level 1

              Sorry for the delayed response. I've been out. Thank you for your help on this!

              I'm working through an issue this solution created. If I enter "9/30" in the "Month Ended" text box, it auto corrects both the "Month Ended" and "Due Date" boxes, but to 2017 dates. I'm assuming that's because we are in 2017 now. I'm not sure how you could "divine" that the user meant "9/30/2016" instead of 9/30/2017 though...maybe by comparing the "Month Ended" to "Today's Date" and prompting a dialogue box if "Month Ended" was after today's date asking the user whether that was the date they wanted to enter...

              • 4. Re: Date Validation Bug Assistance
                -MaddAddaM- Level 1

                Here is what I have pop up to deal with the issue discussed above. Probably not the best solution but hopefully it will prompt people to correct the date if it wasn't what they wanted. As a side note, I also have the "dateToday" field in the form which is why I just call it here. Just wanted to share in case others had similar problems.


                var MonthEnded = getField("Month Ended").valueAsString;

                var Today = getField("dateToday").valueAsString;

                var d1 = util.scand("mm/dd/yyyy",MonthEnded);

                var d2 = util.scand("mm/dd/yyyy",Today);





                app.alert({cMsg:"The month you are completing the form for is after today's date. Please adjust if not correct.\n\n" + "Remember the date format must be entered as 'mm/dd/yyyy'.", nIcon:1});