4 Replies Latest reply on Sep 21, 2010 12:08 PM by Harris927

    Custom format script that keeps leading zeros & requires exact character length

    Harris927

      I'm needing a script that would only allow a certain number format in a text field or else the user cannot tab out of that field. The problem is if I just select the number format and choose what I need it drops all leading zeros. What I need is to require exactly 7 characters, no more and no less, in the format "0000.00" If someone tries to type anything other then that I don't want them to be able to exit out of the field until it is corrected. But I can't seem to find anything that works like that. I've got the leading zeros to not drop by using printf("%07.2f"); I was thinking this would limit characters to 7 and move decimal point 2 places but the only thing it seems to do is let me keep the leading zeros, I can still make the format however I want it. I'm sure what I put is wrong, just not sure what all I need to make this work.

      Thanks in advance for your help on this, I really appreciate it!

        • 1. Re: Custom format script that keeps leading zeros & requires exact character length
          George_Johnson MVP & Adobe Community Professional

          I have no interest in showing you how to prevent from tabbing out of a field because I believe that's evil, but you can set up a document-level function like the following and call it from a custom Keystroke script and have most of what you want:

           

          // Document-level function

          // Call from Keystroke event of a text field

          function Dig42_ks() {

           

              // Define non-commited regular expressions (dddd.dd)
              var re = /^\d{0,4}(\.\d{0,2})?$/;
             
              // Get all of the characters the user has entered
              var value = AFMergeChange(event);

           

              // Allow field to be cleared
              if(!value) return;

           

              if (event.willCommit) {
                 
                  // Define regular expression (dddd.dd)
                  re = /^\d{4}\.\d{2}$/;

           

                  if (!re.test(value)) {  // If final value doesn't match, alert user and try again

           

                      app.alert("Your error message goes here.");


                      // Set focus back to this field
                      event.target.setFocus();
                  }
              } else {  // not commited

           

                  // Only allow characters that match the regular expression
                  event.rc = re.test(value);
              }
          }

          • 2. Re: Custom format script that keeps leading zeros & requires exact character length
            Harris927 Level 1

            Ok, that would be great. Not being able to tab out was all I could think to do but an alert message will work just as good. I've added a Document-level function and tried to call it using a custom keystroke script but I must be doing something wrong because it's not working. It accepts the script without errors but its still letting me put it in any format I want. I may be putting something in wrong though. Which part needs to go in the keystroke script? I'm sorry for the dumb questions, I'm pretty new to this. Thanks again for your help!

            • 3. Re: Custom format script that keeps leading zeros & requires exact character length
              George_Johnson MVP & Adobe Community Professional

              The code in the Keystroke script should be:

               

              // Call the document-level function

              Dig42_ks();

               

               

              If the function is in place, it should work fine. If you have code in any other field event (format, validate, etc.) remove it.

              • 4. Re: Custom format script that keeps leading zeros & requires exact character length
                Harris927 Level 1

                Ok I was putting it in wrong. That worked great, that was exactly what I was needing! Thanks so much for your help, I really appreciate it!