10 Replies Latest reply on May 16, 2011 12:22 PM by George_Johnson

    Need a script

    former_321

      Hope someone can help.    AcrobatX Standard

       

      I have a field titled --- Withdraw Date1 --- I have the fomat set to date MM/DD/YY

       

      I need to NOT allow the user to enter Saturdays or Sundays.   I also need the user NOT to be able to enter Holidays ( a list of dates that I establish)

       

      I figure I need a custom validation script and I know where to put that etc.

       

      Any help would be appreciated.

        • 1. Re: Need a script
          George_Johnson MVP & Adobe Community Professional

          In general, you'd convert the field value to a date object using the util.scand method. You can then use the getDay date method to see what the day of the week is. If it's Saturday or Sunday, reject the entry by setting event.rc to false, or otherwise not allowing the entry.

           

          In order to reject the holidays, you can place them in the array, and then loop through the array and see if the date is equal to any of the holiday dates.

           

          If you get stuck, post whatever code you've come up with so we can suggest changes.

          • 2. Re: Need a script
            former_321 Level 1

            Thanks George, but I should have been clear.

             

            I am new to scripting, and although I have some OLD programming experience and can follow any logic; I am at a loss as to the conventions of java.

            I understand looping and used to do a lot of BASIC.

             

            I got tagged to create 51 differenrt forms and this is the last one.

             

            I have it done to the point that any date can be entered, and I don't know if I have the time to learn all about Javascript.

             

            var d1 = util.scand("mm/dd/yy",WithdrawDate1)


                getDay(d1)

             

            Sorry to be a pest, but now what?

            • 3. Re: Need a script
              George_Johnson MVP & Adobe Community Professional

              OK, I'll try to keep this straightforward and clear. The following is a barebones custom Validate script that should get you started:

               

              // Custom Validate script for date field
              (function () {
              
                  // Get the field value
                  var sDate = event.value;
              
                  // Don't do anything if the field is blank
                  if (!sDate) return;
              
                  // Convert the string to a date
                  var d = util.scand("mm/dd/yy", sDate);
              
                  // Get the day of the week
                  var day = d.getDay();
              
                  // If it's a Saturday (5) or Sunday (6), reject it
                  if (day === 5 || day === 6) {
              
                      // Alert the user
                      app.alert("You my not specify a Saturday or Sunday", 3);
                      
                      // Reject the value
                      event.rc = false;
              
                      // No need to proceed
                      return;
                  }
              
                  // Check to see if it's a holiday here
                  
              })();
              

               

              The first and last lines prevent the unnecessary creation of global variables and allow you use return to stop execution when needed. If you provide more information on what holidays you want to include, someone could suggest code to deal with them.

              1 person found this helpful
              • 4. Re: Need a script
                former_321 Level 1

                Thank you so much Geroge.

                 

                I tried that and it was excluding Fridays and Saturdays, made a few minor edits and that part seems to be working perfectly.

                 

                The dates that I want to exclude are 05/30/11, 07/04/11, 09/05/11, 10/10/11, 11/11/11, 11/24/11, 12/26/11,

                01/02/12, 01/16/12, 02/20/12, 05/28/12, 07/04/12, 09/03/12, 10/08/12, 11/12/12, 11/22/12, and 12/25/12.

                 

                Here is the scrip as I have it entered so fac to exclude the weekends.

                 

                 

                // Custom Validate script for date field
                (function () {

                 

                    // Get the field value
                    var sDate = event.value;

                 

                    // Don't do anything if the field is blank
                    if (!sDate) return;

                 

                    // Convert the string to a date
                    var d = util.scand("mm/dd/yy", sDate);

                 

                    // Get the day of the week
                    var day = d.getDay();

                 

                    // If it's a Saturday (6) or Sunday (7), reject it
                    if (day === 6 || day === 7) {

                 

                        // Alert the user
                        app.alert("You may not specify a Saturday or Sunday", 3);
                       
                        // Reject the value
                        event.rc = false;

                 

                        // No need to proceed
                        return;
                    }

                    // Check to see if it's a holiday here
                   
                })();

                • 5. Re: Need a script
                  George_Johnson MVP & Adobe Community Professional

                  Sorry about that typo, but note that getDay returns 0 for Sunday, 1 for Monday, ..., and 6 for Saturday. So my earlier code was rejecting Fridays and Saturdays, and your code will only reject Saturdays.

                   

                  I'll post some code for the holidays in a bit...

                  • 6. Re: Need a script
                    George_Johnson MVP & Adobe Community Professional

                    Here's a corrected script with the holiday checking code included:

                     

                    (function () {
                    
                        // Get the field value
                        var sDate = event.value;
                    
                        // Don't do anything if the field is blank
                        if (!sDate) return;
                    
                        // Convert the string to a date
                        var d = util.scand("mm/dd/yy", sDate);
                    
                        // Get the day of the week
                        var day = d.getDay();
                    
                        // If it's a Saturday (6) or Sunday (0), reject it
                        if (day === 6 || day == 0) {
                    
                            // Alert the user
                            app.alert("You may not specify a Saturday or Sunday", 3);
                            
                            // Reject the value
                            event.rc = false;
                    
                            // No need to proceed
                            return;
                        }
                    
                        // Create a date string in the mm/dd/yy format
                        sD = util.printd("mm/dd/yy", d);
                    
                        // Define the holidays in an object
                        var oHolidays = {"05/30/11" : "Memorial Day - 2011",
                                         "07/04/11" : "Independence Day - 2011",
                                         "09/05/11" : "Labor Day - 2011",
                                         "10/10/11" : "Veterans Day - 2011",
                                         "11/11/11" : "Columbus Day - 2011",
                                         "11/24/11" : "Thanksgiving Day - 2011",
                                         "12/26/11" : "Christmas Day - 2011",
                                         "01/02/12" : "New Year\'s Day - 2012",
                                         "01/16/12" : "Martin Luther King Day - 2012",
                                         "02/20/12" : "Washington\'s Birthday - 2012",
                                         "05/28/12" : "Labor Day - 2012",
                                         "07/04/12" : "Independence Day - 2012",
                                         "09/03/12" : "Labor Day - 2012",
                                         "10/08/12" : "Veterans Day - 2012",
                                         "11/12/12" : "Columbus Day - 2012",
                                         "11/22/12" : "Thanksgiving Day - 2012",
                                         "12/25/12" : "Christmas Day - 2012"
                                        };
                    
                        // Check to see if the date matches a holiday
                        if (sD in oHolidays) {
                    
                            // Alert the user
                            app.alert("You entered: " + oHolidays[sD] + "\r\rYou may not specify a Holiday.", 3);
                            
                            // Reject the value
                            event.rc = false;
                    
                            // No need to proceed
                            return;
                         }
                        
                    })();
                    
                    • 7. Re: Need a script
                      former_321 Level 1

                      THANK YOU GEORGE !!!!!!!!!!!!!!!!!!!1

                       

                      This works perfectly !!!!!!!!!!!!!!!

                      • 8. Re: Need a script
                        George_Johnson MVP & Adobe Community Professional

                        Except 05/28/12 will be Memorial Day, not Labor Day.

                        • 9. Re: Need a script
                          nmjfaulknerII Level 2

                          Does this only work in X?  I pasted as a VALIDATE script in a test PDF using LCD 9 (have Acrobat Standard 9 as reader) and it does nothing.

                          • 10. Re: Need a script
                            George_Johnson MVP & Adobe Community Professional

                            This is for a form created in Acrobat, not LiveCycle Designer.