8 Replies Latest reply on Nov 17, 2010 12:32 PM by ktr927

    Help with Validating Field

    ktr927

      I'm trying to put a script in a submit button that validates a few fields to make sure they have a certain format. Everything works good if I don't actually go into the field and enter data, if I leave it blank it submits fine. Which is ok, they aren't required fields I just need the right format if a user does enter data in it. Well right now if I enter something in the field and the format is wrong, the error message generates like it's supposed to. But if I go back and erase what's in the field and press Submit again, the error keeps generating even when the field is blank again. I have no idea what I'm doing wrong. I'm just wondering if there's a way to put something like, if this field is Null thats ok. but if it isn't and format does not match generate error message.

       

      Any help would be greatly appreciated, thanks in advance!

        • 1. Re: Help with Validating Field
          George_Johnson MVP & Adobe Community Professional

          It would help if you showed the script you're currently using.

          • 2. Re: Help with Validating Field
            ktr927 Level 1

            This is the script I'm using:

             

            var strMsg = "Invalid Format";
            var v = /^\d{2}\.\d{2}$/;
            var i = getField("text1").valueAsString;

             

                 if (getField("text1").value != "")  {
                      bOK = v.test(i);
                 }

             

                 if (!bOK) {
                      app.alert(strMsg); }

             

                      else {

                           if (bOK) {

                              

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

            • 3. Re: Help with Validating Field
              George_Johnson MVP & Adobe Community Professional

              The following makes more sense to me. Does it behave as you want?

               

               

               

              var strMsg = "Invalid Format";
              var v = /^\d{2}\.\d{2}$/;
              var i = getField("text1").valueAsString;

               

              bOK = v.test(i);

               

              if (!bOK) {
                  app.alert(strMsg);

              } else {

                  mailDoc({

                      bUI: true,

                      cTo: "example@email.com"

                  });

              }

              • 4. Re: Help with Validating Field
                ktr927 Level 1

                No, when I put that in it gives the error message even if I never enter that field and put anything in it. For some reason adding the

                if (getField("text1").valueAsString != "") {bOK = v.test(i); }     is the only thing that at least lets someone submit the form without ever entering data in the field. But once data is entered even if I go back and clear out the field it still generates error message until something is entered that has the right format.

                 

                I'm not sure how to tweak the script to say if field is empty or cleared, its ok. Is there a way to just clear that field if the user enters it wrong?

                • 5. Re: Help with Validating Field
                  George_Johnson MVP & Adobe Community Professional

                  OK, that's a little more clear, I think.

                   

                  To be sure, you're saying if the text field is not blank, you want to enforce the correct formatting before allowing the submit to take place, but an empty value is OK and should not prevent the submit. Is that correct?

                  • 6. Re: Help with Validating Field
                    ktr927 Level 1

                    Yes, that's correct. I tried using this.resetForm(field) to reset that field after the error message goes off, which did clear the field. But its still giving the error message after the field is cleared and nothing is in it. It's like it's holding the value of the last thing that was entered even if the user erases it.

                     

                    I'm kind of new to this so I may just not be understanding how it's all working. Thanks for your help!

                    • 7. Re: Help with Validating Field
                      George_Johnson MVP & Adobe Community Professional

                      OK, try this:

                       

                       

                      var strMsg = "Invalid Format";
                      var v = /^\d{2}\.\d{2}$/;
                      var i = getField("text1").valueAsString;

                       

                      bOK = v.test(i);

                       

                      if (!bOK && i !== "") {
                          app.alert(strMsg);

                      } else {

                          mailDoc({

                              bUI: true,

                              cTo: "example@email.com"

                          });

                      }

                      • 8. Re: Help with Validating Field
                        ktr927 Level 1

                        That worked perfect!! Thank you so much, that also helped me understand how to use the && correctly, never could get it to work before.

                        Thanks again, I really appreciate it!