Expand my Community achievements bar.

SOLVED

Generate Random Number in a Numberic Field

Avatar

Level 2

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 Accepted Solution

Avatar

Correct answer by
Level 10

You would have to use a random function from javascript.

For example

var ran_number=Math.random()*5;

Jasmin

View solution in original post

9 Replies

Avatar

Correct answer by
Level 10

You would have to use a random function from javascript.

For example

var ran_number=Math.random()*5;

Jasmin

Avatar

Level 2

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

Avatar

Level 2

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:

This is what the author told me about the code:

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

Avatar

Former Community Member

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);

Avatar

Level 2
Hello,
The following is the Helper Function variable that you told me to create under File>Form Properties>Variables

I ran the code in the numeric field

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

Is there a step that I am missing?

thanks for the help,

-vargs

Avatar

Former Community Member

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.

Avatar

Level 2

Thanks for your patience,

I tried it once more and recieved the same error.

The following code is under my variable "HelperFunctions"

and this is the code under my text field:

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

thanks for your help,

-vargs

Avatar

Former Community Member

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.

Avatar

Level 2

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

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