15 Replies Latest reply on Oct 13, 2010 8:50 PM by ktr927

    Script to validate a field when submit button pressed not working

    ktr927

      I have a script to validate certain fields and generate an error message if fields are blank or not filled out correctly. Everything works fine except for one of them. I'm trying to generate an error message if one of the fields have an invalid format in it. For example, the format should be ''XXXX.XX'' (all numbers) so if they enter ''XXXX'' the error message will generate and the form will not submit. I have a function setup under this particular field and it works but unfortunately users are ignoring the error message and submitting the form anyway and this field really needs to be right before it's submitted.

      This is what I'm using for it:

       

      var bCancel = false;

      var strMsg = "";

       

      if (getField("Tracking").value == /^\d{0,4}(\.\d{0,2})?$/) {

      bCancel = false; }

       

      if (getField("Tracking").value != /^\d{0,4}(\.\d{0,2})?$/) {

      strMsg = "Invalid Format. Please correct before continuing. (EX: 0000.00)";

      bCancel = true; }

       

      So the problem is it generates the error message now even if the format is correct. I'm sure I did something wrong but I wrote the format the same way (/^\d{0,4}(\.\d{0,2})?$/) when I ran a custom format script under this field and it worked fine so I'm not sure what I'm doing wrong. I'm also including this part of the script with the rest of the field validations in the submit button. So altogether it looks something like this (just with a lot more fields, didn't include all of them bc it gets pretty redundant)

       

      var bCancel = false;

      var strMsg = "";

       

      if (getField("Tracking").value == /^\d{0,4}(\.\d{0,2})?$/) {

      bCancel = false; }

       

      if (getField("Employee Name").value >= 1) {

      bCancel = false; }

       

      if (getField("AcctNbr1").value >= 1) {

      bCancel = false; }

       

       

      if (getField("Tracking").value != /^\d{0,4}(\.\d{0,2})?$/) {

      strMsg = "Tracking field format is invalid. Please correct before continuing. (EX: 0000.00)";

      bCancel = true; }

       

      if (getField("Employee Name").value == "") {

      strMsg="At least one required field was empty on export. Please fill in the required fields (highlighted) before continuing.";

      bCancel = true; }

       

      if (getField("AcctNbr1").value == "") {

      strMsg="At least one required field was empty on export. Please fill in the required fields (highlighted) before continuing.";

      bCancel = true; }

       

      if (bCancel){

      app.alert(strMsg);

      } else {

      this.mailDoc({

      bUI:       true,

      cTo:      "example@email.com",

      cSubject: "Form Returned: Maintenance Form"});

       

      }

       

      I'm fairly new to scripting, especially in Adobe, so I probably didn't do a great job with all of it. Please let me know if there's anything I need to change to get it working better. Thanks in advance for your help!

        • 1. Re: Script to validate a field when submit button pressed not working
          try67 MVP & Adobe Community Professional

          You're not using the regular expression correctly.

          Have a look here to see how it's done:

          http://w3schools.com/js/js_obj_regexp.asp

          • 2. Re: Script to validate a field when submit button pressed not working
            ktr927 Level 1

            Wow, I've got a lot to learn, lol. Ok I will try my best, hopefully I do it right! Thanks for your help!

            Oh, is it ok for me to put this with the rest of the script I'm using or do I need to add another javascript in the submit button? 

            Thanks again.

            • 3. Re: Script to validate a field when submit button pressed not working
              try67 MVP & Adobe Community Professional

              You can include it in the same script.

              There's actually not that much you need to change. Have a look mostly at the

              test() method.

              • 4. Re: Script to validate a field when submit button pressed not working
                Harris927

                Something's still not right, it's still generating the error message even when the format is correct. I just don't understand why when I use the script in the keystroke field it works fine, once format is correct it lets me move on. But when I use pretty much the same thing in the submit button no matter what it generates the error message.  I know I'm doing something wrong just can't put my finger on it. Adobe accepts the script without any problems so there's just something else I'm missing.

                • 5. Re: Script to validate a field when submit button pressed not working
                  George_Johnson MVP & Adobe Community Professional

                  Not only are you not using the regular expressions correctly (not using the test method, as mentioned previously), the regular expression you're using is not appropriate for testing against a final value. They may be fine for using in a Keystroke script while the field value is not committed, but not for checking a final value.

                  • 6. Re: Script to validate a field when submit button pressed not working
                    George_Johnson MVP & Adobe Community Professional

                    By the way, is it a requirement that all seven characters are required: four digits to the left of the decimal point, the decimal point, and two digits to the right of the decimal point?

                    • 7. Re: Script to validate a field when submit button pressed not working
                      ktr927 Level 1

                      Well I figured I was doing something wrong, I'm very new to scripting so I know it's gonna take me a little bit to learn all the rules and ways to go about it the right way. I really appreciate the help though, this forum has already taught me a lot :-)

                       

                      But yes, I do need it to be exactly 7 characters in those positions. I did try to run the test method but it's still not working correctly. I took out the bCancel method completely and just tried doing it based on the link the previous post provided, including running the test script. It's possible I'm making the mistake when I try to point it to the specific field it needs to check, the tutorial wasn't having to call out specific fields so I was doing that part on my own, I'm sure I wrote it wrong. What's the best way to go about doing that?

                       

                      Thanks again for your help.

                      • 8. Re: Script to validate a field when submit button pressed not working
                        George_Johnson MVP & Adobe Community Professional

                        OK, below are two regular expressions that may do what you want:

                         


                        var re1 = /^\d{4}\.\d{2}$/;
                        var re2 = /^[1-9]\d{3}\.\d{2}$/;

                         

                        The first, re1, will match any four digits, followed by a decimal point, followed by two digits. A potential problem with this is it will match a string like "0001.23", which may not be what you want. If you don't want it to match a string with leading zeroes, you can use the second one, re2. It will match any digit from 1 to 9, followed by any three digits, followed by a decimal point, followed by two digits.

                         

                        The test method of a regular expression takes a string as an argument and will return true if it matches the regular expression, or false if it doesn't. Here are some example outputs given various field values and methods of getting the field values:

                         

                        // Text1 field value is 0123.45

                         

                        // Get the field value.

                        // The variable num will be a number, not a string

                        // num will equal 123.45

                        var num = getField("Text1").value;

                         

                        // Test regular expression against num

                        bOK = re1.test(num);   // returns false

                         

                        // Now get the field value as a string

                        // num will equal "0123.45"

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

                        bOK = re1.test(num);   // returns true

                         

                        // Now use the other regular expression

                        bOK = re2.test(num);  // returns false

                         

                        // Text1 field value is now 1234.56

                        // num will equal "1234.56"

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

                        bOK = re2.test(num);   // returns true

                         

                        To summarize, if you want to not allow leading zeroes, get the field value using the valueAsString field property, and use a regular expression that does not match a leading zero. You should probably change the regular expression you're using in the Keystroke script if you want to prevent the user from entering a number with any leading zeroes.

                        • 9. Re: Script to validate a field when submit button pressed not working
                          ktr927 Level 1

                          Ok a little closer but still not quite right! I'm still getting the error message even when format is correct. I found out the reason though is for some strange reason when I'm using the regular expression /^\d{4}\.\d{2}$/  it wants the value of the field to be 4444.22. That's all it will accept. If it's 1234.56 it generates the error message. Also, I do not want the leading zeros to drop so the first regular expression should be what I need. This is what I have right now, I included the part that's working fine for me as well just in case that would help it make more sense.

                           

                          var bCancel = false;

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

                           

                          if (getField("text1.value >= 1){
                          bCancel = false; }
                          if (getField("text2").value >= 1) {
                          bCancel = false; }

                          if (getField("text1").value == "") {
                          bCancel = true;
                          strMsg="At least one required field was empty on export. Please fill in the required fields (highlighted) before continuing.";  }
                          if (getField("text2").value == "") {
                          bCancel = true;
                          strMsg="At least one required field was empty on export. Please fill in the required fields (highlighted) before continuing.";  }
                          if (!re.test(num)) {
                          strMsg="Invalid Format"; }

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

                          if (bCancel)  { app.alert(strMsg);
                          } else {
                          this.mailDoc  ({
                          bUI:       true,
                          cTo:      "example@email.com",
                          cSubject: "Maintenance Form",
                          cMsg:     "Form Returned: Maintenance Form.pdf\n\n\
                          The attached file is the filled-out form. Please open it to review the data." });  }
                          • 10. Re: Script to validate a field when submit button pressed not working
                            George_Johnson MVP & Adobe Community Professional

                            I've corrected, cleaned up, and simplified that code a bit. Not all of it makes sense to me, but give it a try. Note that if you want to retain the leading zeroes, you need to get the field value using the "valueAsString" property, not the "value" property.

                             

                             

                            var bCancel = false;

                            var strMsg = "At least one required field was empty on export. Please fill in the required fields (highlighted) before continuing.";

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

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

                            bOK = re.test(num);

                             

                            if (getField("text1").value >= 1) {

                                bCancel = false;

                            }

                             

                            if (getField("text2").value >= 1) {

                                bCancel = false;

                            }

                             

                            if (getField("text1").value == "") {

                                bCancel = true;

                            }

                             

                            if (getField("text2").value == "") {

                                bCancel = true;

                            }

                             

                            bCancel = bOK;

                             

                            if (bCancel) {

                                app.alert(strMsg);

                            } else {

                                mailDoc ({

                                    bUI: true,

                                    cTo: "example@email.com",

                                    cSubject: "Maintenance Form",

                                    cMsg: "Form Returned: Maintenance Form.pdf\n\n\The attached file is the filled-out form. Please open it to review the data."

                                });

                            }

                            • 11. Re: Script to validate a field when submit button pressed not working
                              ktr927 Level 1

                              Ok I deleted my script and just copy/pasted yours but now when I press submit nothing happens, it doesn't generate a message or try and email it.

                              The reason I thought I had to include the strMsg after each if statement is because I needed 2 different error messages, one if a required field was missing and the other that pointed them to that specific field that needed the correct format. I know it made my code really long but I couldn't think of any other way to make it work. Would I be able to attach my pdf in here, would that help any?

                              Thanks again for your help, I really appreciate it!

                              • 12. Re: Script to validate a field when submit button pressed not working
                                ktr927 Level 1

                                Also, is there any other way to write the code instead of making re = /^\d{4}\.\d{2}$/

                                It seems like that is what's making it think the value needs to be 4444.22 instead of letting the value be anything as long as the format is XXXX.XX.

                                It makes no sense to me why this wouldn't work because it works perfect in the function Dig42_ks() I added for something else. But I thought I could give it a try just to rule out if that was actually causing the problem or not, just didn't know another way to write it. Thanks again!

                                • 13. Re: Script to validate a field when submit button pressed not working
                                  George_Johnson MVP & Adobe Community Professional

                                  Yes, it's really hard to tell what's going on without seeing the file. You can post it here, but if you can't post it elsewhere (e.g., acrobat.com), feel free to send it to me by email (see my profile) and I'll take a look.

                                  • 14. Re: Script to validate a field when submit button pressed not working
                                    ktr927 Level 1

                                    Ok I emailed you the form. Let me know what you think. Thanks!

                                    • 15. Re: Script to validate a field when submit button pressed not working
                                      ktr927 Level 1

                                      Wow, i FINALLY got it to work! Instead of /^\d{4}\.\d{2}$/ I had to use re = /^\d\d\d\d\.\d\d$/  and that worked. Sometimes Adobe can be really frustrating! Thanks for all your help, I appreciate it!