6 Replies Latest reply on Jan 4, 2015 12:16 PM by Adobe troubleshooting

    Java script Novice

    Adobe troubleshooting

      I'm a novice with JavaScript but wanted to know how difficult it would be if I had a form with Date of Birth and Date Form Completed then have Age auto fill based on those dates?

        • 1. Re: Java script Novice
          George_Johnson MVP & Adobe Community Professional

          It's not too difficult, but you have to know exactly how you want to calculate the age. It is sometimes rounded to the nearest year, or must be one day after the birth day in order to increment.

          • 2. Re: Java script Novice
            Adobe troubleshooting Level 1

            Thanks George, I think we just want it to show current age at the time the form is completed. It's one of those things that's easy to type in but people forget so we wanted to have it fill in after entering those dates.

            • 3. Re: Java script Novice
              gkaiseril MVP & Adobe Community Professional

              The following script with error checking will compute the age based on the current date. The script includes error checking for the existence of needed support scripts.

               

              // document level funcitons;
              function GetField(cName) {
              // get field object with error checking;
              var oField = this.getField(cName);
              if(oField == null) app.alert("Error accessing field " + cName + "\nPlease check the fieild name and its existance.", 1, 0);
              return oField;
              } // end GetField function;

              function Scand(cFormat, cDate) {
              // convert date sting for foramt;
              // includes error checking;
              var oDate = util.scand(cFormat, cDate);
              if(oDate == null) app.alert("Error converting " + cDate + " with formatting of " + cFormat + "\nPlease check the date is a vialid date and has the correct formatting.", 1, 0);
              return oDate;
              } // end Scand funciton;
              // end document level functions;


              // custom field calculation for age field;
              // compute age in years using year month and date;

              var cDateFormat = "mm/dd/yyyy"; // format of date field;
              var cDateField = "DOB.1"; // dob field name;

              function FunctonExist(cFunctionName) {
              var bExist = true;
              if(eval("typeof " + cFunctonName + " != \"fuction\"")) {
                app.alert("Missing document level function " + cFunctionName, 0, 0);
                bExist = false;
              }
              return bExist;
              }

              var bFunctions = true;
              bFunctions = bFunctions || FunctionEist("GetField");
              bFunctions = bFunctions || FunctionEist("Scand");
              bFunctions = bFunctions || FunctionEist("Date2Num");

              var nAge = 0; // clear age result;

              if(bFunctions) {
              var oDateField = GetField(cDateField);
              if(oDateField.valueAsString != "") {
                var oDate = Scand(cDateFormat, oDateField.valueAsString)
                var oNow = (new Date());
                var nAge = oNow.getFullYear() - oDate.getFullYear(); // difference in full years;
                // adjustments for today's month or date in date of birth before birth month and birth day;
                if(oNow.getMonth() < oDate.getMonth()) nAge--; // todays month before birth month;
                if(oNow.getMonth() == oDate.getMonth() && oNow.getDate() < oDate.getDate()) nAge--; // same month as birth month but todays's date before birth date;
                }
              }
              event.value = nAge;
              // end custom field calculation;

              • 4. Re: Java script Novice
                Adobe troubleshooting Level 1

                This is awesome and just what I'm looking for. Only issue is the age will not change if the DOB changes unless I open the Age box to edit. I'm not sure I'm plugging in the script in the correct place.

                • 5. Re: Java script Novice
                  gkaiseril MVP & Adobe Community Professional

                  The script should be placed in the custom JavaScript calculation are on the calculation tab.

                   

                  Are you getting any errors in the JavaScript console?

                  • 6. Re: Java script Novice
                    Adobe troubleshooting Level 1

                    It works perfect after putting it into the calculations box. Thank you very much for your help.