5 Replies Latest reply on Jan 4, 2013 12:48 PM by gkaiseril

    How do you force a text field to update to input date before validating?

    StephenCarterMCP

      I have this code:

       

      // Custom On Blur script for txtWeekEndingDate

      (function () {

          var weekEnding = new Date(event.value);

          var weekday=new Array(7);

          weekday[0]="Sunday";

          weekday[1]="Monday";

          weekday[2]="Tuesday";

          weekday[3]="Wednesday";

          weekday[4]="Thursday";

          weekday[5]="Friday";

          weekday[6]="Saturday";

          var dayOfWeek = weekday[weekEnding.getDay()];

          var strWeekEnding = weekEnding.toLocaleDateString();

       

       

          // Do nothing if the field is blank

          if (!event.target.value) return;

       

       

          // If the value is not a valid email address...

          if (dayOfWeek!="Sunday") {

             

              // ...let the user know the value is not a valid end of week date...

              app.alert({cMsg:"You entered, '" + strWeekEnding + ".' Please enter a Sunday date." , nIcon:3, nType:0, cTitle:this.documentFileName});

       

       

              // ...and set the focus back to this field

              event.target.setFocus();

       

       

          } // endif dayOfWeek!="Sunday"

       

       

      })();

       

      The problem is if the user enters something like "1/1" into the input box (that has a date format set in the text box's properties of "mm/dd/yyyy") it sets the weekEnding variable to "undefined." Without the validation script it correctly changes the date to "1/1/2013," but it appears that this change only happens after the OnBlur script runs. How can I force the update to happen before the script is run?

        • 1. Re: How do you force a text field to update to input date before validating?
          gkaiseril MVP & Adobe Community Professional

          You could use the custom key stroke JavaScript, but date entry and validation by user JavaScript is not simple. 

           

          Consider, if one enters 1/2 is it January 2 or February 1?

           

          Not only is the formatting of the display important, but also the input format and how JavaScript uses the inputted value. What you see for the formatted display might not be the value the user entered.

           

          Have you tried to look at the value of your variables within the function?

           

           

           

          (function () {
              var weekEnding = new Date(event.value);
              var weekday=new Array(7);
              weekday[0]="Sunday";
              weekday[1]="Monday";
              weekday[2]="Tuesday";
              weekday[3]="Wednesday";
              weekday[4]="Thursday";
              weekday[5]="Friday";
              weekday[6]="Saturday";

             console.show();
             console.clear();
             console.println("event.value: " + event.value);
             console.println("weekEnding: " + weekEnding);

            
              var dayOfWeek = weekday[weekEnding.getDay()];
          console.println("dayofWeek: " + dayOfWeek);
              var strWeekEnding = weekEnding.toLocaleDateString();
              // Do nothing if the field is blank
              if (!event.target.value) return;
              // If the value is not a valid email address...
              if (dayOfWeek!="Sunday") {
                  // ...let the user know the value is not a valid end of week date...
                  app.alert({cMsg:"You entered, '" + strWeekEnding + ".' Please enter a Sunday date." , nIcon:3, nType:0, cTitle:this.documentFileName});
                  // ...and set the focus back to this field
                  event.target.setFocus();
              } // endif dayOfWeek!="Sunday"
          })();

           

           

          When I enter 1/1 I find the "weekEnding" has a value of "Invalid Date"!

           

          If one enters 1/1/2012 you will get a value for "weekEnding".

          • 2. Re: How do you force a text field to update to input date before validating?
            StephenCarterMCP Level 1

            That doesn't really answer my question. I guess I need to understand better what the events do. I am much more familiar with VB and VBA than JavaScript. In VB there are many events to trap input. My assumption may be wrong that on_blur event equates to the LostFocus event in VBA. Putting code into the LostFocus event would mean that the field has already been validated and updated by the application and the value of the field has been set. This seems not to be the case with on_blur.

             

            If I have set a date format for the field ('mm/dd/yyyy') and an input of '1/1' is made the Adobe application changes the value automatically to '01/01/2013.' My question is in what event can I extract the completed value of '01/01/2013' from the control? Because at this point creating JavaScript for validation and update routines based on the input of a control would be useless unless I can be assured that I can get the final value (after automatic updates) of the control to validate against. (It is always been my believe that if the application has a built-in feature I should always use it rather than reinventing the "wheel.")

            • 3. Re: How do you force a text field to update to input date before validating?
              gkaiseril MVP & Adobe Community Professional

              There maybe other things going on like "auto complete", which I have turned off for security reasons.

               

              Have you read the Acrobat JavaScript API Reference on Form event processing?

               

              Maybe that will clarify the order of the processing of events.

              • 4. Re: How do you force a text field to update to input date before validating?
                George_Johnson MVP & Adobe Community Professional

                When an entry of "1/1" gets converted to "01/01/2013", it is only for what gets displayed in the field. The underlying field value will remain at "1/1". The point is the field value doesn't change, just what gets displayed in the field. This means that there is no direct method to get the text that's displayed.

                 

                You could add a custom Validate script that changes an entered value of "1/1" to "01/01/2013". You'd then just get the field value to retrieve it. If you want more help with this approach, post again.

                • 5. Re: How do you force a text field to update to input date before validating?
                  gkaiseril MVP & Adobe Community Professional

                  You could also use the keystroke event to manipulate the partial input value to force the field value to some default value as the individual enters the data.

                   

                  As I previously stated the displayed value is not the actual value of the field. This fact can cause problems or make some problems much easier.

                   

                  For example, one can have a time sheet where a user enters the in and out times and the daily time interval is computed in minutes and stored in the field as minutes but is displayed as hours and minutes for a formatted display. Then one only needs to sum the daily times to get the total for a week and the formatting of the field will display in what ever custom format you want.

                   

                  Or a user can enter a date in a different format than the field's property and the field will display the date in the field's format but the value of the field will be the user entered string. This can cause a lot of problems unless one converts the field's value to the JavaScript date object and then uses the various methods of the date object to perform the scripting tasks.