21 Replies Latest reply on Aug 15, 2013 4:21 PM by LAIHDT Dan

    Custom validation script - coding help needed

    LAIHDT Dan Level 1

      Hi guys,

       

      I have a conundrum with a form I'm trying to scrap together a custom validation script for.

      Coding is not my strength but I can copy & paste and get the idea but for this exercise it seems the Javascript is beyond me.

       

      I have a field in a form which a user needs to enter a 'valid' Noosh code.

      The Noosh code consists of 5 numbers, a letter then two numbers.

        E.G: 56413P01 or 34265P05 or 84172P13

      The field doesn't need to check against codes in a list, it simply needs to reduce input error to do with an extra number or dropping a number etc. Basically it needs to pertain to the above format before submission.

       

      I've been trying to muck around with this code which I feel is on the right track (pulled from another source):

       

      // Custom Keystroke script:
      // RegExp for keystrokes
      var re = /^\d{0,3}(\-)?\d{0,3}(\-)?\d{0,4}$/
      // run only if field not committed
      if (event.willCommit == false)
      {
      // test keystroke against the RegExp
      if(re.test(event.change) == false)
      {
      // we have an invalid keystroke
      app.beep();
      // force field to clear
      event.rc = false;
      }
      }
      
      // Custom Format script 
      // RegExp input (999-999-9999) for format
      var re = /^(\d{3})[-](\d{3})[-](\d{4})$/
      if(re.test(event.value) == true)
      {
      // drop "-" and add "."
      event.value = RegExp.$1 + "." + RegExp.$2 + "." + RegExp.$3;
      }
      
      // Custom Validation script:
      // only run when field not blank
      if (event.value != "")
      {
      // RegExp for validation 999-999-9999
      var re = /^(\d{3})([-])(\d{3})([-])(\d{4})$/
      if(re.test(event.value) == false)
      {
      app.alert("Enter 10-digit phone number beginning with area code (e.g., 916-456-8910) - It will be converted to the format 916.456.8910 as that is how it will appear on the business card");
      event.rc = false;
      }
      }
      

       

       

      In actual fact the script doesn't seem to work for the format it's alerting to but in my naive opinion it seems to have the right elements, keystroke something, validation check and an alert.

       

      My problem is massaging this to make it fit to my requirements, besides the fact that it didn't work I couldn't decode the character expressions ( E.G: var re = /^(\d{3})([-])(\d{3})([-])(\d{4})$/)

      Am I even on the right track?

      Can someone help me format it to suit my needs?

        • 1. Re: Custom validation script - coding help needed
          MarkWalsh Level 4

          To get you started, this should be the validation expression you are looking for.

           

          var re = /^\d{5}[A-Z]\d{3}$/

           

          ^ = Begining of string

          \d{5} = 5 digits

          [A-Z] = 1 capital letter

          \d{3} = 3 digits

          $ = end of string

          1 person found this helpful
          • 2. Re: Custom validation script - coding help needed
            LAIHDT Dan Level 1

            Thanks for your help Mark.

             

            It's beginning to make sense from the string perspective.

            I changed the last section to two digits and inserted it in the script but I think the script above is broken somewhere.

             

            I'm not sure that the custom keystroke is working (I'm not sure what that should do), I know for sure the Validation is broken because I keep getting a validation alert unless the alert is the thing that's broken then I'm in real trouble.

             

            Do you know what it might be?

            • 3. Re: Custom validation script - coding help needed
              LAIHDT Dan Level 1

              Fixed, I trashed most of it as I realised the middle section was pertaining to the old code with full stops between the text.

               

              I also trashed the keystroke validation as it remembered the last correct entry forcing a user not to be able to leave the field blank.

               

              Thanks for your help Mark.

              • 4. Re: Custom validation script - coding help needed
                LAIHDT Dan Level 1

                Actually, one more thing if anyone can help.

                 

                I'm wanting to apply this validation check to a submit button which already has a lock and mail pdf script attached to it.

                 

                I want the submit button to do this:

                 

                 

                // Custom Validation script for Noosh code
                // only run when field not blank
                
                var noosh = this.getField("INT_NooshCode")
                if (noosh.value != "")
                {
                // RegExp for validation 99999X99
                var re = /^\d{5}[A-Z]\d{2}$/
                if(re.test(noosh.value) == false)
                {
                app.alert("Sorry, we can't start a project without a Noosh code. \n\nPlease enter a valid Noosh code EG: 34256P02");
                noosh.rc = false;
                }
                }
                

                 

                If it fails then stop for the user to fix OR if it's successful do this:

                 

                // This code will pull information from certain fields,
                // flatten the pdf then attach and email pdf to pre-selected email addresses
                
                var ProjectTitle = getField("ProjectTitle").value
                var Brand = getField("Brand").value
                var Name = getField("Name").value
                var Noosh = getField("INT_NooshCode").value
                
                for (var i = 0 ; i < this.numFields ; i++) {
                 var f = this.getField(this.getNthFieldName(i)) ;
                 if (f.type != "Submit") // Change f.type to button name in form that the action is applied to
                 {
                 f.readonly = true ;
                 }
                 }
                
                this.mailDoc({
                
                cTo: "email@email.com",
                
                cBcc: "email2@email.com",
                
                cSubject: "NEW JOB: "+Brand+" - "+ProjectTitle+" - "+Noosh,
                
                cMsg: "Thanks "+Name+" for sending through this job."
                
                });
                

                 

                Message was edited by: LAIHDT Dan - updated with extra help info

                • 5. Re: Custom validation script - coding help needed
                  mrfale67 Level 2

                  I would put all your 'OR' stuff into a function;

                  so:

                  function OR_Stuff( comma-separated params ) {

                  ...code here...

                  }

                   

                  then in your RegEx test:

                   

                  if(re.test(noosh.value) == false) {

                  app.alert("Sorry, we can't start a project without a Noosh code. \n\nPlease enter a valid Noosh code EG: 34256P02");

                  }

                  else{

                  // make a call to OR_Stuff()

                  OR_Stuff(comma-separated params);

                  }

                   

                  I noticed your "noosh.rc" which I think is wrong because fields don't have 'rc' prop.

                  • 6. Re: Custom validation script - coding help needed
                    LAIHDT Dan Level 1

                    That makes sense but I think my formatting has hit a snag somewhere.

                    I'm getting formal parameter missing errors.

                     

                    I currently have this:

                     

                    // Custom Validation script for Noosh code
                    // only run when field not blank
                    
                    var noosh = this.getField("INT_NooshCode")
                    if (noosh.value != "")
                    {
                    // RegExp for validation 99999X99
                    var re = /^\d{5}[A-Z]\d{2}$/
                    if(re.test(noosh.value) == false) {
                    app.alert("Sorry, we can't start a project without a Noosh code. \n\nPlease enter a valid Noosh code EG: 34256P02");
                    }
                    else{
                    OR_Stuff(comma-separated params);
                    }
                    
                    function OR_Stuff( comma-separated params ) {
                    
                    // This code will pull information from certain fields,
                    // flatten the pdf then attach and email pdf to pre-selected email addresses
                    
                    var ProjectTitle = getField("ProjectTitle").value
                    var Brand = getField("Brand").value
                    var Name = getField("Name").value
                    var Noosh = getField("INT_NooshCode").value
                    
                    for (var i = 0 ; i < this.numFields ; i++) {
                     var f = this.getField(this.getNthFieldName(i)) ;
                     if (f.type != "Submit") // Change f.type to button name in form that the action is applied to
                     {
                     f.readonly = true ;
                     }
                     }
                    
                    this.mailDoc({
                    
                    cTo: "email@email.com",
                    
                    cBcc: "email2@email.com",
                    
                    cSubject: "NEW JOB: "+Brand+" - "+ProjectTitle+" - "+Noosh,
                    
                    cMsg: "Thanks "+Name+" for sending through this job."
                    
                    });
                    
                    }
                    
                    • 7. Re: Custom validation script - coding help needed
                      mrfale67 Level 2

                      Delete "comma-separated params" which was just a proxy to tell you how to deal with any passing parameters. I think you don't need params here, so just leave OR_Stuff( ) in both places you find it.

                      Then I would add several semicolons (I see at least 6 missing in particular) at line ends. However, if you don't get any errors, you can leave it so. 

                      • 8. Re: Custom validation script - coding help needed
                        LAIHDT Dan Level 1

                        Thanks mrfale67.

                         

                        I feel like I'm very close but my formatting sucks. The first part of the script executes but it doesn't do the 'OR' part of the command when the validation is correct. Can someone help me format this to work correctly?

                         

                        Current code:

                         

                        var re = /^\d{5}[A-Z]\d{2}$/
                        if(re.test(INT_NooshCode.value) == false)
                        {
                        this.getField("RequiredAlert").display = display.visible;
                        this.getField("NooshTick").display = display.hidden;
                        app.alert("Sorry, we can't start a project without a Noosh code. \n\nPlease enter a valid Noosh code EG: 34256P02");
                        }
                        else{
                        var ProjectTitle = getField("ProjectTitle").value
                        var Brand = getField("Brand").value
                        var Name = getField("Name").value
                        var Noosh = getField("INT_NooshCode").value
                        
                        for (var i = 0 ; i < this.numFields ; i++) {
                         var f = this.getField(this.getNthFieldName(i)) ;
                         if (f.type != "Submit") // Change f.type to button name in form that the action is applied to
                         {
                         f.readonly = true ;
                         }
                         }
                        }
                        this.mailDoc({
                        
                        cTo: "dwilliams@au.loreal.com",
                        
                        cBcc: "spaolini@au.loreal.com",
                        
                        cSubject: "NEW JOB: "+Brand+" - "+ProjectTitle+" - "+Noosh,
                        
                        cMsg: "Thanks "+Name+" for sending through this job.\n\n"+"Your Job Order Form is attached for the Design Team to action.\n"+"Write an email below with a quick summary of your job or request."
                        
                        });
                        
                        
                        • 9. Re: Custom validation script - coding help needed
                          mrfale67 Level 2

                          I think I was wrong. Only 5 semicolons to add because var re = /^\d{5}[A-Z]\d{2}$/ should stay so. Though, if you get an error, add a semicolon here too.

                          Try this:

                           

                          function OR_Stuff() {

                           

                                    var ProjectTitle = getField("ProjectTitle").value;

                                    var Brand = getField("Brand").value;

                                    var Name = getField("Name").value;

                                    var Noosh = getField("INT_NooshCode").value;

                           

                                    for (var i = 0 ; i < this.numFields ; i++) {

                                              var f = this.getField(this.getNthFieldName(i));

                                              if (f.type != "Submit") // Change f.type to button name in form that the action is applied to

                                                        {

                                                                  f.readonly = true;

                                                        }

                                    }

                           

                           

                                    this.mailDoc({

                           

                                    cTo: "email",

                           

                                    cBcc: "email",

                           

                                    cSubject: "NEW JOB: "+Brand+" - "+ProjectTitle+" - "+Noosh,

                           

                                    cMsg: "Thanks "+Name+" for sending through this job."

                           

                                    });

                          }

                           

                          var re = /^\d{5}[A-Z]\d{2}$/

                          if (re.test(INT_NooshCode.value) == false) {

                                    this.getField("RequiredAlert").display = display.visible;

                                    this.getField("NooshTick").display = display.hidden;

                                    app.alert("Sorry, we can't start a project without a Noosh code. \n\nPlease enter a valid Noosh code EG: 34256P02");

                          }

                          else {

                                    OR_Stuff();

                          }

                           

                          A suggestion: try to understand the meaning of the code (and any suggestion given here) otherwise this will be the first and only one script of your life... This meaning that if you don't learn by doing, you don't make any progress and sooner or later you will give up.

                          • 10. Re: Custom validation script - coding help needed
                            LAIHDT Dan Level 1

                            Thanks for your help mrfale67 I really appreciate it. I guess this happens a lot here hey, blow-ins wanting someone to make up code for their pretty pdf.

                             

                            I'm trying my hardest not to do that by doing exactly what you suggested but it only takes me so far without having to dive right into learning proper syntax. Man that sounds like a cop out as I type it.

                             

                            Anyway, your latest code seems to get stuck somewhere, it doesn't validate the field. Thanks for your input, I'm stoked I got this far.

                             

                            Is anyone able to dot the i's and cross the t's for me?

                            • 11. Re: Custom validation script - coding help needed
                              mrfale67 Level 2

                              Let's take your "re.test(INT_NooshCode.value)".

                              I hope you would finally dive into JavaScript syntax and get to grips with its constructs. You can start from here: 

                              http://www.w3schools.com/jsref/jsref_regexp_test.asp

                               

                              MarkWalsh showed you how to deal with RegExp (http://www.w3schools.com/jsref/jsref_obj_regexp.asp) which sounds the most difficult part of your code to me.

                               

                              Now, you just have to make small adjustments to have it work properly (the i's dots and t's crosses, as you say).

                               

                              In "re.test(INT_NooshCode.value)" you are testing something as you may have guessed by the 'test' method's name itself. In particular, your are testing your previously defined RegExp against INT_NooshCode.value.

                              Have you ever defined INT_NooshCode.value in you code? I don't see it anywhere.

                              So, I guess you pretend INT_NooshCode.value to be the value the field you are validating is assuming each time.

                              If you are placing all your code in the validation event of INT_NooshCode field, then replace "INT_NooshCode.value" with "event.value". This is necessary because you are not writing a doc script. In a document script, you would generally do this:

                              var myField = this.getField("INT_NooshCode");

                              ...re.test(myField.value)...

                               

                              However, if you tried the same in a validation event script, your "myField.value" would be the past value of your field and not the one you are currently validating. The new value your user has just put into your field can only be intercepted by "event.value".

                              Try this and come back here if needed.

                              1 person found this helpful
                              • 12. Re: Custom validation script - coding help needed
                                LAIHDT Dan Level 1

                                Jee, I think with every post of yours mrfale67 I begin elated then move to frustration, then onto a long burst of inquisitiveness, then frustration again, then small success then new ideas.

                                 

                                Thanks for all your help, while I didn't think I'd get this to work you were right, there were only some small errors. I had actually tried defining INT_NooshCode previously but it didn't work. I attacked it again and removed a .value from somewhere.

                                 

                                Well in the end the script works perfectly, even though its probably a complete mess with the double validation (on blur of the field its self and again on submit) I went a step further and moved the OR_Stuff() function to the document scripts and the if, else to the button.

                                 

                                Thanks for your perseverance mrfale67, I appreciate it.

                                 

                                To put the icing on the cake, I'm going to nest the entire code in another 'if' which requires a precursor of a field to be visible before running, if not then then run OR_Stuff.

                                 

                                How am I able to test the visibility of a field?

                                 

                                Here is what I have written up as an example (button script on submit), the first if, else is what I'm after.

                                The button now sends the pdf regardless of if the field is visible or not.

                                 

                                var noosh = getField("INT_NooshCode");
                                if (noosh.display == false)
                                {
                                sendMail();
                                }
                                
                                else{
                                
                                
                                // previous validation code below
                                
                                
                                var re = /^\d{5}[A-Z]\d{2}$/
                                if (re.test(noosh.value) == false) {
                                
                                          this.getField("RequiredAlert").display = display.visible;
                                          this.getField("NooshTick").display = display.hidden;
                                          app.alert("Sorry, we can't start a project without a Noosh code. \n\nPlease enter a valid Noosh code EG: 34256P02");
                                }
                                else {
                                          this.getField("RequiredAlert").display = display.hidden;
                                          this.getField("NooshTick").display = display.visible;
                                          sendMail();
                                }
                                }
                                
                                • 13. Re: Custom validation script - coding help needed
                                  mrfale67 Level 2

                                  Now, man, you are gone so far that it's time for you to bookmark this one:

                                  http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf

                                   

                                  Not only bookmark it. Save it. Print it.

                                   

                                  The answer to your last question lays at

                                   

                                  Field --> Field properties --> display

                                  1 person found this helpful
                                  • 14. Re: Custom validation script - coding help needed
                                    LAIHDT Dan Level 1

                                    mrfale67 has done it again!

                                     

                                    Piece of cake:

                                     

                                    var noosh = getField("INT_NooshCode");

                                    if (noosh.display == display.hidden)

                                    {

                                    sendMail();

                                    }

                                     

                                    else{

                                     

                                    var re = /^\d{5}[A-Z]\d{2}$/

                                    if (re.test(noosh.value) == false) {

                                     

                                              this.getField("RequiredAlert").display = display.visible;

                                              this.getField("NooshTick").display = display.hidden;

                                              app.alert("Sorry, we can't start an internal print project without a Noosh code. \n\nPlease enter a valid Noosh code EG: 34256P01");

                                    }

                                    else {

                                              this.getField("RequiredAlert").display = display.hidden;

                                              this.getField("NooshTick").display = display.visible;

                                              sendMail();

                                    }

                                    }

                                    • 15. Re: Custom validation script - coding help needed
                                      hdouet84

                                      Can anyone help? I've created a text field for a date with the format mm/dd/yyyy but need to create another text field that takes the data keyed and formats it to be mmddyyyy (without the slashes) and hide this text box.  Can this be done?

                                      • 16. Re: Custom validation script - coding help needed
                                        mrfale67 Level 2

                                        Go to http://acrobatusers.com/

                                         

                                        there you can put me this question directly (choose "Find Experts").

                                         

                                        Anyway, I'd suggest to open a new thread for each new question.

                                        • 17. Re: Custom validation script - coding help needed
                                          LAIHDT Dan Level 1

                                          mrfale67 I'm adding more variables to this form.

                                           

                                          What command do I use to call the face value of a drop box and what do I use to call an export value?

                                          • 18. Re: Custom validation script - coding help needed
                                            Mohammad Irfan Level 1

                                            for face value

                                             

                                            ddExample.getItemAt(0,false);

                                             

                                            0 is index of entry

                                             

                                            for export value

                                             

                                            ddExample.getItemAt(0,true)

                                            • 19. Re: Custom validation script - coding help needed
                                              LAIHDT Dan Level 1

                                              Thanks for the answer Mohammad but that code means I have to re-determine the selection I want, that doesn't work when I user selects it does it?

                                               

                                              Here's my example:

                                               

                                              LIST (face & export value):

                                              PPD - email1

                                              ACD - email2
                                              CPD - email3

                                               

                                              If a user selects "ACD" from the list, I want to use that value in the subject line of the email when they submit the form, I also want to use the Export Value of the same selection as the email address.

                                              • 20. Re: Custom validation script - coding help needed
                                                mrfale67 Level 2

                                                LAIHDT Dan,

                                                I am not a moderator of this forum but I think we can't make this thread endless to cover all your different requests until you have one.

                                                If you need my assistance, please open a new thread at http://acrobatusers.com/

                                                (you don't need to create a new account).

                                                There you can put me this question directly (choose "Find Experts" and look for "Ricardo Falegnami").

                                                 

                                                Hope it helps.

                                                • 21. Re: Custom validation script - coding help needed
                                                  LAIHDT Dan Level 1

                                                  Alright it looks like I've pushed my luck.

                                                   

                                                  We've built a bloody amazing code in my books Ricardo, thanks.