9 Replies Latest reply on Jul 23, 2010 7:00 AM by avargs

    Generate Random Number in a Numberic Field

    avargs Level 1

      Hello,

       

      I am wondering if there is a way to Generate a random number in a numeric Field or any other type?

       

      I am currently using LiveCycle Designer ES 8.2

       


      thanks,

       

      -Vargs

        • 1. Re: Generate Random Number in a Numberic Field
          Jasmin Charbonneau Level 4

          You would have to use a random function from javascript.

           

          For example

          var ran_number=Math.random()*5;

           

          Jasmin

          • 2. Re: Generate Random Number in a Numberic Field
            avargs Level 1

            Thanks for the post, I was researching further into what I wanted to do  and found this post

             

            http://www.acrobatusers.com/forums/aucb … ?pid=60135

             

            What  I want to do with this random number is generate it every time someone  opens a new form.  This form will be saved on a persons computer and  worked on.  So I don't want the number to change every time the file is  open, only when the form doesn't have a unique number already in it.

             

            I  believe the answer is the following but I am not sure if it will do the  task I want to perform. Here is the following code

             

            Quote from  the post by user Jonom:

             

            "Here's a script I've used on the  Initialize event of a form. It checks to see if it already has a value  and if so doesn't generate a new number in case people save the form and  reopen it:

             

            The number is unique to the second, so two people  would have to open the form at the same second to get the same value..."

             

            ****************************************************
            Code:

             

            if  (this.rawValue == null) {
                var d = new Date();
                //divide  result by 1000 to get seconds and round off
                this.rawValue =  parseInt(d / 1000);
            }
            else {
                this.rawValue = rawValue;
            }

             

            end  quote

             

            ****************************************************
            How  can I put in this code into a numeric field. I made a numeric field in  Livecycle Designer ES 8.2
            and tried to add in code but was not sure  which event to put it in or where exactly it should be placed.

             


            Thanks,

             

            -Vargs

            • 3. Re: Generate Random Number in a Numberic Field
              avargs Level 1

              I found out through the author of that code that it is indeed what I am looking for but I really need you help trying to get it working.

               

              The updated code is:

              if (this.rawValue == null)
              {
                  var d = new Date();
                  //divide by 1000 to get seconds and trim decimals
                  this.rawValue = Math.floor(d / 1000);
              }
              else
              {
                  this.rawValue = rawValue;
              }

              This is what the author told me about the code:

              The code goes on the Initialize event of the field. I use a text field but numeric will be fine, you'll just need to set a display pattern to get rid of commas.

              It's not a random number, the Date() function returns the number of milliseconds from the Epoch (January 1, 1970). I'm then dividing by 1000 to get seconds so it's a smaller number to deal with. One potential advantage of this is that given the number on the form you can extract the time and date.


              Here is the difficulty I am having trying to implement it...

               

              I used a Text Field but it would not let me access the Initialize state  to insert in the code.

               

              I then reverted back to using a numeric  field and added in the code right below
              "form1.#pages[0|.Page2.button1::  initialize - FormCalc, client)

               

              I then tried to preview the pdf  and recieved the following error:

               

              "syntax error near token "{" on  line 2 column 1.

               

              thank you for your help,

               

              -vargs

              • 4. Re: Generate Random Number in a Numberic Field
                Jerry62712 Level 1

                I've gotten that, but not always.  The problem was the creation of the date object at that event wasn't acceptable for javascript creation of objects.  What I did was create an external to the form (global) javascript library with a function that created the data object and passed it back to the invoking routine.

                 

                In the variables section of the hierarchy I added a file I called HelperFunctions.  It's serving as an ".js" file.  Inside of it I have:

                function setDate(mm,dd,yyyy){
                    return new Date(yyyy,(mm-1),dd);
                }

                 

                function newDate(){
                    return new Date();
                }

                 

                These routines can be accessed anywhere.  In your form you will have an object and in a script you could have:

                var prior10Date = HelperFunctions.setDate(7,3,2010);

                • 5. Re: Generate Random Number in a Numberic Field
                  avargs Level 1
                  Hello,
                  The following is the Helper Function variable that you told me to create under File>Form Properties>Variables

                  function setDate(mm,dd,yyyy){
                      return new Date(yyyy,(mm-1),dd);
                  }

                   

                  function newDate(){
                      return new Date();
                  }

                  I ran the code in the numeric field

                  if (this.rawValue == null)
                  {
                      var d = new Date();
                      //divide by 1000 to get seconds and trim decimals
                      this.rawValue = Math.floor(d / 1000);
                  }
                  else
                  {
                      this.rawValue = rawValue;
                  }

                  and it still gave me the same error message as before.

                   

                  Is there a step that I am missing?

                   

                  thanks for the help,

                   

                  -vargs

                  • 6. Re: Generate Random Number in a Numberic Field
                    Jerry62712 Level 1

                    You still have the creation (if you are technical it's call instantiation) of the date object in your script.  Change the

                    var d = new Date();

                    to

                    var d = HelperFunctions.setDate(7,3,2010);

                     

                    This is assuming you created the HelperFunctions in the right place.  I right click the root under the hierarchy window and pick "insert script object".  It creates one at the botton of that window called "untitled script object" under "Variables".  I rename that to the "HelperFunctions" and then click it.  The script editor window will then hold the two functions I gave you.

                     

                    setDate will create a date object and populate it with some date.  You can make the constants in my example variables if you want to create a date based on the current date.

                    • 7. Re: Generate Random Number in a Numberic Field
                      avargs Level 1

                      Thanks for your patience,

                       

                      I tried it once more and recieved the same error.

                      The following code is under my variable "HelperFunctions"

                      function setDate(mm,dd,yyyy){
                          return new Date(yyyy,(mm-1),dd);
                      }



                      function newDate(){
                          return new Date();
                      }

                      and this is the code under my text field:

                      if (this.rawValue == null)
                      {
                          var d = HelperFunctions.setDate(7,3,2010);
                          //divide by 1000 to get seconds and trim decimals
                          this.rawValue = Math.floor(d / 1000);
                      }
                      else
                      {
                          this.rawValue = rawValue;
                      }

                      I am still troubled as to what I am doing wrong.

                       

                      thanks for your help,

                       

                      -vargs

                      • 8. Re: Generate Random Number in a Numberic Field
                        Jerry62712 Level 1

                        Delete your variable and recreate it per my prior post.  Make sure you use the "insert script object" option when you right click the root in the hierarchy window.  Throw in some alerts (xfa.host.messageBox("text telling you where you are");) before you call the subroutine and within it just to make sure.  Post your form here so the real experts can look at it.

                        • 9. Re: Generate Random Number in a Numberic Field
                          avargs Level 1

                          Hello,

                           

                          Thank you so much for being patient with me.

                          I found out this morning that the main reason I was having trouble was because

                          All sample codes here are in JavaScript but you set the language to FormCalc, that's why you get an error.

                          Once I changed the code from FormCalc to JavaScript, the problem was fixed.

                           

                          I am so sorry for all the trouble the confusion may have caused you.

                          I really appreciate all you have done to try and help me.

                           

                          Luckily the problem has been fixed and this question has been put to rest.

                           

                          thanks again,

                           

                          -vargs