Skip to main content
Known Participant
February 11, 2017
Answered

Need help with this script. Take a look.

  • February 11, 2017
  • 1 reply
  • 623 views

I'm using this script to generate a random number. It works fine but I would like to modify it to include a No, Cancel, Yes button. As you can see I figured out placing the "3" into the script gives me the three buttons. But, the random number still gets placed into the m12 field, regardless of what button is pressed. Can someone show me how to make the modification so I'll be able to have the choice as to what I want to do? Here's my existing script.

      case "ID Number":       

// custom funcitons to generate random integers and random strings;

function RandomInt(nMax, nMin) {

// generate a random integer between the nMin and nMax values

if(typeof nMin == "undefined") nMin = 0;

nMin = parseInt(nMin);

nMax = parseInt(nMax);

return Math.floor(Math.random() * (nMax - nMin)) + nMin;

} // end RandomInt function

function RandomString(cValues, nLength) {

// generate a random string of length nLength from string of cValues;

nlength = parseInt(nLength);

// convert string of values to an an array;

var aValue = cValues.split("");

// array for results of random characters;

var aResult = new Array()

// random intiger;

var nRandom;

// populate array with 6 random values;

for(i = 0; i < nLength; i++) {

// get random number;

nRandom = RandomInt(aValue.length);

// insert random letter into array;

aResult = aValue[nRandom];

} // end of populate array;

// convert array of values to a string;

return aResult.join("");

} //

// end custom funcitons;

// possible values for items in the field

var cString = "ABCDEF0123456789";

// generate a random string of values;

var cResult = RandomString(cString, 7);

// display the random string

// populate a field with the value;

app.alert("Enter unique Proposal Number:\n\n" + cResult, 0, 3);  ///this produces the No, Cancel, Yes buttons

this.getField("m12").value = cResult;

         break;     

Thank you all for taking the time to read my post. If you can help, please do.

This topic has been closed for replies.
Correct answer try67

I found the code on the user forum. I cut it and pasted it into my Acrobat program just to see what it would do. Just trying things that look interesting. And it worked. I don't plan on writing javascript for a living, I'm just playing around with different things for myself. I looked through the script above and I still don't know what an "if-condition" is or how to write the correct code to make it do what I'd like. I just thought to add the option to not apply the random number.


Whether or not it's for a living, it will be well worth your time to learn the basic concepts behind writing this code.

The answer to your question is to change those two lines of code to this:

if (app.alert("Enter unique Proposal Number:\n\n" + cResult, 0, 3)==4)

    this.getField("m12").value = cResult;

1 reply

try67
Community Expert
Community Expert
February 11, 2017

Have a look at the return value of app.alert for each one of the buttons, and then add an if-condition so that the next line is only executed if Yes is clicked (assuming that's what you want to do).

pdfUser1Author
Known Participant
February 11, 2017

Thanks for the reply. The only thing is, I don't know enough about javascript to do what you're suggesting, I'm still a novice. What is an "if-condition"? I would gladly insert it if I had one. What would it look like? Is there a reference that I could use to better understand?

try67
Community Expert
Community Expert
February 11, 2017

This is a very basic thing. If you're going to be writing code in JS then you must be familiar with the basic syntax of the language.

How were you able to come up with the code above (which actually includes an if-condition), if you don't know what it is?