11 Replies Latest reply on Feb 22, 2012 8:16 AM by try67

    Date range

    Milton123321 Level 1

      Good afternoon,

       

      I would like to write a script that turns a field into read only if an employee puts a particular date range in 2 fields. Here is the proposed code; however,  I am at a loss with the syntax

       

      if(this.getField("date1").value>08/09/10 && this.getField("date2").value<08/30/10){

           this.getField("field1").readonly=true;

           }

           else

           this.getField("field1").readonly=false.

        • 1. Re: Date range
          try67 MVP & Adobe Community Professional

          Date objects can only be compared when converted to milliseconds.

          You can use something like this:

           

          var startDate = util.scand("dd/mm/yyyy", "08/09/2010");

          var endDate = util.scand("dd/mm/yyyy", "08/30/2010");

          var d1 = util.scand("dd/mm/yyyy", this.getField("date1").value);

          if(d1==null) {

          // there was a problem converting the field's value to a Date object...

          } else {

               if (d1.getTime()>startDate.getTime() && d1.getTime()<endDate.getTime()) {

                    this.getField("field1").readonly=true;

               } else {

                    this.getField("field1").readonly=false;

               }

          }

          • 2. Re: Date range
            Milton123321 Level 1

            Try,


            Thanks.

             

            Below is a sample code based off what you provided. I am having trouble executing it?

             

            var fy12start = util.scand("yyyymmdd","201101001")

            var fy12end = util.scand("yyyymmdd","20120930")

            var fy13start = util.scand("yyyymmdd","201201001")

            var fy13end = util.scand("yyyymmdd","20130930")

            var fy14start = util.scand("yyyymmdd","201301001")

            var fy14end = util.scand("yyyymmdd","20140930")

             

            var setDate = util.scand("yyyymmdd", this.getField("8a. Start Date").value)

               

            if(this.getField("EvalPeriod").value=="FY12" && setDate.getTime()>fy12end.getTime()){

                app.alert("This is an invalid value",1);

                }

             

            I am entering 20140520 and it is not detecting that the date is greater than the end of the selected fiscal year. What am I missing?

            • 3. Re: Date range
              try67 MVP & Adobe Community Professional

              Are you getting any error messages in the JS console?

              Also, if your fields are purely numeric, you should use the

              valueAsString-property instead of the value-property.

              • 4. Re: Date range
                Milton123321 Level 1

                Try,

                 

                Value as string property works. The Counsole was giving me setDate is null error.

                 

                Where can I read more about javascript properties,methods and objects?

                 

                Why did the value property not work? Instead, the valueAsString worked? Since all the values are numeric in the code, i would think the value property would work. valueAsString coverts it to a string?

                • 5. Re: Date range
                  try67 MVP & Adobe Community Professional

                  General JS tutorials and reference:

                  http://w3schools.com/js/

                  https://developer.mozilla.org/en/JavaScript/Reference

                   

                  JS in Acrobat API Reference:

                  http://www.adobe.com/devnet/acrobat/javascript.html

                   

                  For a large variety of tutorials on how to use scripts in Acrobat see also:

                  http://acrobatusers.com/tutorials

                   

                  To answer your question: When the value is purely numeric, the value

                  property returns a number object, which can't be used in the scand method.

                  And valueAsString will return the value as a string, which is what we need.

                  • 6. Re: Date range
                    Milton123321 Level 1

                    Try,

                     

                    thanks!

                     

                    I have another quick question. I use the "custom calcuation script" often when inputting code. For example, I have this code below

                     

                    var fy12start = util.scand("yyyymmdd","20111001")

                    var fy12end = util.scand("yyyymmdd","20120930")

                    var fy13start = util.scand("yyyymmdd","20121001")

                    var fy13end = util.scand("yyyymmdd","20130930")

                    var fy14start = util.scand("yyyymmdd","20131001")

                    var fy14end = util.scand("yyyymmdd","20140930")

                     

                    var setDate = util.scand("yyyymmdd", this.getField("8a. Start Date").valueAsString)

                     

                    if(this.getField("EvalPeriod").value=="FY12" && setDate.getTime()<fy12start.getTime() || setDate.getTime()>fy12end.getTime()){

                        app.alert("The date input is outside the range of FY12",1);

                        event.value=""

                        }

                     

                    if(this.getField("EvalPeriod").value=="FY13" && setDate.getTime()<fy13start.getTime() || setDate.getTime()>fy13end.getTime()){

                        app.alert("The date input is outside the range of FY13",1);

                        event.value=""

                        }

                     

                    if(this.getField("EvalPeriod").value=="FY14" && setDate.getTime()<fy14start.getTime() || setDate.getTime()>fy14end.getTime()){

                        app.alert("The date input is outside the range of FY14",1);

                        event.value=""

                        }

                     

                    I am having issues excecuting this code. It somehow runs the 1 line of code even though I select FY14

                    • 7. Re: Date range
                      try67 MVP & Adobe Community Professional

                      No, JS code runs the same, but since these are different event, the values

                      of various things could be different.

                      Is this the calculation/validation script of EvalPeriod?

                       

                      Generally speaking, the calculation script is supposed to assign a value to

                      the field by using event.value.

                      A validation field is not supposed to set event.value, only to

                      accept/reject it by setting the value of event.rc to true/false, resp.

                      • 8. Re: Date range
                        Milton123321 Level 1

                        The goal of the script is to validate whether or not the user inputted a date wthin a date range

                         

                        For Example, if the user selected Fy12, the valid entry is between 10/01/11 to 09/30/12.

                         

                        If the user inputs a date outside the range, the field should reject the entry.

                         

                        var fy12start = util.scand("yyyymmdd","20111001")

                        var fy12end = util.scand("yyyymmdd","20120930")

                         

                        var setDate = util.scand("yyyymmdd", this.getField("8a. Start Date").valueAsString)

                         

                        if(this.getField("EvalPeriod").value=="FY12" && setDate.getTime()<fy12start.getTime() || setDate.getTime()>fy12end.getTime()){

                            app.alert("The date input is outside the range of FY12",1);

                            event.value=""

                         

                        This is what I am currently using; however, it seems using a validation script would be more prudent?

                        • 9. Re: Date range
                          try67 MVP & Adobe Community Professional

                          But which field is it? I don't see that you're use event.value as the

                          current value entered by the user. You're only using the value properties,

                          which is not correct. If this is the value of "8a. Start Date" then you

                          shouldn't use getField("8a. Start Date").value, but event.value.

                          • 10. Re: Date range
                            Milton123321 Level 1

                            Try,

                             

                            I see what you are saying.

                             

                            Yes. what the code is trying to do is see if the 8a. start date field is in the valid range. that is what the "event.value" method is changing.

                             

                            Assume we are refering to the same field "Bob" and that i am using a custom calculation script within field "bob".

                             

                            what is wrong with me changing the value of field bob using this code?

                             

                            this.getField("bob").value="haha"

                             

                            isnt this synomous with

                             

                            event.value="haha"?

                            • 11. Re: Date range
                              try67 MVP & Adobe Community Professional

                              They are only the same in the calculation script... During the validation

                              script the entered value has not been committed yet (since it's still being

                              validated), so using getField() will retrieve the previous value of the

                              field. The value it had before the user started editing it.

                               

                              You should read the reference of the "event" object in the Acrobat JS API

                              Reference.