3 Replies Latest reply on Jul 30, 2012 1:39 PM by conny2254

    Automatically add colon to text field with javascript

    conny2254

      I am very new to using javascript.  I am very familiar with vba and MS Excel and I have used macros to automatically insert a colon into a time string and I am wondering if anyone can help me to do this in an acrobat text field.  for example if I enter 0900 i would want the output to be 09:00.  I also would want to always have the output be a 4 digit string and would like to use military time starting at 0000 to 2400 (not 2359).  If anyone can point me to the correct code or where to start that would be very much appreciated.

       

      Thank you.

       

       

      Chris

        • 1. Re: Automatically add colon to text field with javascript
          George_Johnson MVP & Adobe Community Professional

          It depends on how you want it to work. After a user makes an entry, you can either change the actual value of the field or change how the value is displayed in the field. For example, an entry of 0900 can be displayed as 09:00, but the underlying field value is still 0900. You also need to describe what you want to happen if fewer than four characters are entered. For example, should an entry of 01 be auto-converted to 01:00 or 00:01? Should a blank value be shown as blank, or 00:00? You say you always want the output to be a four digit string, but 09:00 is five characters. I'm assuming you also want to limit the entry to just digits, soe that the user is unable to enter something like ABCD, correct?

          • 2. Re: Automatically add colon to text field with javascript
            conny2254 Level 1

            Thank you for the quick reply.  Since I would like to use a calculation to subtract to time values I think I would need to change the actual value of the field.  If fewer than 4 numeric characters is entered I would like to throw a alert or message box stating please enter a 4 digit time string between 0000 and 2400.  A blank value should be shown as blank.  I meant the input would be a 4 digit string and the output should be a 5 digit string.  I would like to limit the entry to just digits.  This is not necessary but if the user did enter the time correctly and used a colon would it be able to work with that as well?  Else I could just force the user to omit the colon if that is easier?  I know all of what I am requesting is possible in VBA so I would assume anything is possible in javascript as well but unfortunately I am too new to know how to go about this yet.  Thank you again for the reply and any more direction is much appreciated.

             

            Chris

            • 3. Re: Automatically add colon to text field with javascript
              conny2254 Level 1

              I have found the below code from a different website.  I think this might be what I am looking for but I am uncertain how to use it?

               

              http://www.codingforums.com/showthread.php?t=239576

               

              Any further assistance is much appreciated.

               

               

              function autoTabTimes(input) {

              var len = input.value.length;

              if (len<3) {

              alert ("Invalid time - re-enter it");

              input.value = "";

              return false;

              }

              if (len==3){

              input.value ="0" + input.value;

              }

              var final = input.value.split("");

              var h = Number(final[0] + final[1]);

              var m = Number(final[2] + final[3]);

              if (h <0 || h >23 || m <0 || m >59) {

              alert ("Invalid time - re-enter it");

              input.value = "";

              return false;

              }

              var f = final[0]+final[1]+":"+final[2]+final[3];

              input.value = f;

              }

               

              function strip(which) {

              var x = which.value;

              x = x.replace(/[^0-9]/g,"");  // allow only digits

              which.value = x;

              }