6 Replies Latest reply on Oct 20, 2010 2:52 PM by ktr927

    Help: Field not accepting leading and ending zeros

    ktr927

      Ok I have been fighting with this script for awhile and everytime I think its fixed I find a new problem, so sorry I keep bothering you guys with this! Hopefully this

      will be the last problem though! I'm trying to validate a field when submit button is pressed that checks to make sure this field has the correct format, which is "XXXX.XX". So far the best expression for this has been /^\d\d\d\d\.\d\d$/

      I've also tried /^\d{0,4}(\.\d{0,2})?$/ but that one has given me a lot of problems, the main thing being it only accepts that field to have 4444.22 in it, anything else, for example 1234.56, generates an error. 

      So now when I use /^\d\d\d\d\.\d\d$/ everything works fine as long as the field doesn't begin or end in zeros. So either 0123.45 or 1234.50 generates an error but 1002.34 submits just fine. I'm needing something to add in the script, either a different expression, or something else that will allow leading or ending zeros. I don't want it to add the zeros though, if the user enters 1234.5 I need the error message to generate, I don't need it to change it to 1234.50.

       

      Thanks in advance for your help!

       

        • 1. Re: Help: Field not accepting leading and ending zeros
          Thom Parker Adobe Community Professional

          The only thing wrong with your first regular expression is that it check for 0 to 4 digits and 0 to 2 digits.  If you want an exact match then it is too general. This is the expression you want:

           

          /^\d{4}\.\d{2}$/

           

          This is exactly the same as /^\d\d\d\d\.\d\d$/,  there is no difference.

           

          These expressions work just fine for leading zeros.  I believe your issue is that you've put formatting on the field.  Get rid of it.  The formatting removes leading and trailing zeros.

           

          If you want to restrict the user from entering anything except the correct format then put this script in the keystroke event

           

          if(!event.willCommit)

          {

             var aFull = event.value.split("");
             aFull.splice(event.selStart, event.selEnd - event.selStart, event.change);
             var strFull = aFull.join("");

               event.rc = (/^\d{0,4}\.?\d{0,2}$/).test(strFull);

          }

           

          Thom Parker
          The source for PDF Scripting Info
          pdfscripting.com

           

          The Acrobat JavaScript Reference, Use it Early and Often
          http://www.adobe.com/devnet/acrobat/javascript.html

           

          Then most important JavaScript Development tool in Acrobat
          The Console Window (Video tutorial)
          The Console Window(article)

          • 2. Re: Help: Field not accepting leading and ending zeros
            ktr927 Level 1

            I have the format set to none in the actual field. I tried adding that script in the keystroke event but it adds a zero if the format isn't right. So I took that out, changed it back to none for format and changed the expression to /^\d{4}\.\d{2}$/ in my submit button. Unfortunately it's still giving me an error message if there's a leading or ending zero. I don't know what's wrong with it. Is there a way to tell it that leading or ending zeros are ok, maybe putting that somewhere other then just using the expression? Thanks for your help!

            • 3. Re: Help: Field not accepting leading and ending zeros
              Thom Parker Adobe Community Professional

              There is nothing wrong with the Keystroke script I provided.  It does not add anything to the data entered.  The only improvement would be to change the event.rc line to

               

                   event.rc = (/^\d{0,4}(\.\d{0,2})?$/).test(strFull);

               

              The behavior you have described is inconsistent with the information you have provided.  There is something going on that you are not telling us about. If its not the field format then it's likely that the field value is being converted into a number before it is being tested with the regular expression.  Exactly how is the regular expression being used in your submit button? And are there any scripts at all on the field.

               

              Thom Parker
              The source for PDF Scripting Info
              pdfscripting.com

               

              The Acrobat JavaScript Reference, Use it Early and Often
              http://www.adobe.com/devnet/acrobat/javascript.html

               

              Then most important JavaScript Development tool in Acrobat
              The Console Window (Video tutorial)
              The Console Window(article)

              • 4. Re: Help: Field not accepting leading and ending zeros
                ktr927 Level 1

                Right now I have nothing at all in the field I'm testing, no scripts and the format is set to none. I started from scratch and just set up a test form with one field called text1 that needs the right format and then a submit button. This is the script I put in the submit button.

                 

                var re =   /^\d{4}\.\d{2}$/;
                var num = getField("text1").value;

                 

                bOK = re.test(num);

                 

                     if (!bOK)  {
                          

                          app.alert("Invalid Format"); 

                     }      

                               else {

                          this.mailDoc({
                          bUI:       true,
                          cTo:      "example@email.com",
                          cSubject: "testing",
                          cMsg:     "testing"});
                        }

                 

                When I use this script and put a leading zero at the beginning or end of the field (0123.45 or 1234.50) I get "Invalid Format" alert. But if I put something like "1234.56" it submits fine.

                 

                Please let me know if there's a better way to do this, thanks!

                • 5. Re: Help: Field not accepting leading and ending zeros
                  Thom Parker Adobe Community Professional

                  Ok, my original thought was that you were using the regExp in a field script, but it's being used on an acquired field value.  So I did some testing and Acrobat is automatically converting the value to a number.  A little unexpected, but there is an easy fix.  Change you code like this:

                   

                  var re =   /^\d{4}\.\d{2}$/;

                  var num = getField("text1").valueAsString;

                   

                  bOK = re.test(num);

                   

                  A lot of pain for a little fix, business a usual for software development;)

                   

                   

                  Thom Parker
                  The source for PDF Scripting Info
                  pdfscripting.com

                   

                  The Acrobat JavaScript Reference, Use it Early and Often
                  http://www.adobe.com/devnet/acrobat/javascript.html

                   

                  Then most important JavaScript Development tool in Acrobat
                  The Console Window (Video tutorial)
                  The Console Window(article)

                  • 6. Re: Help: Field not accepting leading and ending zeros
                    ktr927 Level 1

                    Wow, days of trying to figure it out and thats all it was!! lol. Thank you so much, that's been driving me crazy! :-) I really appreciate it!