4 Replies Latest reply on Jan 13, 2017 11:12 PM by awbrown66

    Birthday Script Error

    awbrown66

      I have the following script in a form, but I haven't dealt with JavaScript in a LONG time, and I'm stuck!  The process I want to happen is for the user to enter their birth date, and then the form displays a message indicating their age, then the field is validated and they move to the next field. I want the field to accept only "mm/dd/yyyy", but the user is allowed to enter just about anything they want, and the field accepts the entry. There is a simple error in the script causing the field not to catch incorrectly formatted field entries. What am I missing?

       

      //Document Level Script:

      function getAge(birthDate) {
          var today = new Date();
          var age = today.getFullYear() - birthDate.getFullYear();
          var m = today.getMonth() - birthDate.getMonth();
          if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
              age--;
          }   
          return age;
      }

       

       

      //Custom Format Script:

      if (!event.value) {
          event.value = "mm/dd/yyyy";
          event.target.display = display.noPrint
      }
      else {
          event.target.display = display.visible;
      }

       

       

      //Validation Script:

      if(event.value != ""){
          event.target.required = false;
      }
      else {
          event.target.required = true;
      }

       

      if (event.value != "") {
      var now = new Date(); // Todays Date
      now.setHours(0);
      now.setMinutes(0);
      now.setSeconds(0);
      now.setMilliseconds(0);
      var d = util.scand("mm/dd/yyyy", event.value); // Date of Birth entry by user

          var age = getAge(d);

      if (d == null) {
        app.alert("Invalid date entry! Please enter your 'Date of Birth' in 'mm/dd/yyyy' format");
        event.rc = false;
      }
          else if (age < 18) {
        app.alert("The date you entered indicates that you are less than 18 years of age!\n\nWe cannot process your application unless you are 18 years of age or older!", 1, 0)
              event.rc = false;
      }
          else {
              app.alert("You have indicated that you are " + age + " years old.\n\nIf this is correct, please move to the next question.\n\nIf it is incorrect, check your date of birth and re-enter it as necessary.", 3, 0)

        event.rc = true; // this is redundant
        event.value = util.printd("mm/dd/yyyy", d); // strict format
      }
      }

      event.target.required = (event.value == "" || !event.rc);

        • 1. Re: Birthday Script Error
          gkaiseril MVP & Adobe Community Professional

          If a user enters a value like "12/10/1956"  is the date "December 10, 1956" or "October 12, 1956"?

           

          You need to have a way to know exactly what the month and date value are or you cannot perform your task.

          • 3. Re: Birthday Script Error
            awbrown66 Level 1

            If you enter 12/10/1956, it displays correctly "12/10/1956".  The calculation section works correctly, it's the entry validation process that has problems.  One could enter "1111" or "aB2CR3" and the field would accept the entry... if one did make such an entry, the calculation does not execute, it just jumps to the next field.  As long as one enters any date in "mm/dd/yyyy" format, the field works great with no errors.

            • 4. Re: Birthday Script Error
              awbrown66 Level 1

              Good luck getting anyone in this place to give you more than a "hit and run" reply unless you plan to pay for services.

               

              Once I had time to go over the script and investigate, I found that I placed the getAge function in the wrong location in the Validation script above.  I've included the corrected Validation script below, in case anyone wants to use it.  The function and format scripts listed above work correctly "as is" in my document.

               

              //Validation Script:

              if(event.value != ""){
                  event.target.required = false;
              }
              else {
                  event.target.required = true;
              }

               

              if (event.value != "") {
              var now = new Date(); // Todays Date
              now.setHours(0);
              now.setMinutes(0);
              now.setSeconds(0);
              now.setMilliseconds(0);
              var d = util.scand("mm/dd/yyyy", event.value); // Date of Birth entry by user

              if (d == null) {
                app.alert("Invalid date entry! Please enter your 'Date of Birth' in 'mm/dd/yyyy' format");
                event.rc = false;
              }

                  else {
              var age = getAge(d);

                  if (age < 18) {
                app.alert("The date you entered indicates that you are less than 18 years of age!\n\nWe cannot process your application unless you are 18 years of age or older!", 1, 0)
                      event.rc = false;
              }
                  else {
                      app.alert("You have indicated that you are " + age + " years old.\n\nIf this is correct, please move to the next question.\n\nIf it is incorrect, check your date of birth and re-enter it as necessary.", 3, 0)

                event.rc = true; // this is redundant
                event.value = util.printd("mm/dd/yyyy", d); // strict format
              }
              }
              }
              event.target.required = (event.value == "" || !event.rc);