3 Replies Latest reply on Dec 11, 2012 9:20 AM by gkaiseril

    Automatic time formatting

    yeungyam Level 1

      I have a text field that I want the user to input a time. I want the field to automatically convert the number into time.

      For example, if the user inputs 856, the text will be converted to 8:56.

      If the user intputs 1224; the text field will display 12:24.

       

      I have taken a look at the thread here:

      http://forums.adobe.com/thread/1103677?tstart=0

      but it doesn't act exactly as I want because in this case the user has to input 4 digits and the column (:).

        • 1. Re: Automatic time formatting
          gkaiseril MVP & Adobe Community Professional

          If you want military time, that is 24 hour time format without the ":" separator, then you need to use a text field and not a "Time" format. You then need to create custom keystroke, validation, and format scripts. If you access the value of the field in another script, you need to get the value as a string unless you want to lose the leading zero.

          • 2. Re: Automatic time formatting
            yeungyam Level 1

            In the text field, I have custom validation

            // 24 hour time validation script for times form 00:00 to 23:59

             

            var RE_24hr = /^(2[0-3]|[01]?[0-9])([0-5]?[0-9])$/;

             

            if(RE_24hr.test(event.value) == false && event.value != "") {

             

            app.alert("Enter time in 24 hour format and a value between 00:00 - 23:59", 1, 0, "Time Value Error");

            }

            and custom format

             

            if(((event.value) >= "001") && ((event.value) <= "959"))

            {event.value = util.printx("9:99", event.value);}

             

            if(((event.value) >= "1000") && ((event.value) <= "2359"))

            {event.value = util.printx("99:99", event.value);}

             

            When I enter 940, it's fine and shows 9:40

            But if I enter 1059, it converts to 10:5

            • 3. Re: Automatic time formatting
              gkaiseril MVP & Adobe Community Professional

              In military time, all times are 4 digits long. If you do not want to lose the leading zero, you must access the value as a string.

               

              If you are going to compare numbers, do not use quotation marks around the value or you are creating a text string and not a floating point number.

               

              I would use RegExp for the custom format. But is this needed if the entered value has been validated?

               

              You have to be using a text field with "Fromat" of "None".

               

              Also you might try the following validation script:

               

              // Military 24 hour time validation script for times form 00:00 to 23:59

              var RE_24hr = /^(2[0-3]|[01]?[0][0-9])([0-5]?[0-9])$/;

              if(RE_24hr.test(event.value) == false && event.value != "") {

              app.alert("Enter time in Military 24 hour format and a value between 0000 - 2359", 1, 0, "Time Value Error");

              event.rc = false;

              }

               

              It requires the leading zero for times less 1000.