Copy link to clipboard
Copied
I have form in which there are 28 Fields in which numbers will be filled in, and in the 29th field greatest number among the 28 number is to be shown.
Like if the fields are populated with
1
5
6
7
9
5
10
50
68
15
15
60
485
58
68
695
58
94
28
38
76
46
86
248
Then the Field should show the greatest number among these i.e 695
1 Correct answer
If you have fields named from 0 to 27, you cannot run your loop from 0 to 28 - unless you have an exception handler. You need to limit the loop to run from 0 to 27. There is another problem with your code which I did not see last time around: You initialize maxValid to "false", but then you test for "true" inside your loop (and set it to "false" if it was true). You don't have a valid value yet on the first iteration, so you need to check for "false" and set the variable to "true" inside the "if
...Copy link to clipboard
Copied
You can use the "Math.max()" function to do that:
All you need to do is call the function with all your values, or use the array approach outlined in the link above:
var theMax = Math.max(Number(this.getField("Field1").value), Number(this.getField("Field2").value), Number(this.getField("Field3").value), ...);
Just keep in mind that if you don't have a number (a "not a number"), or an empty field, this will not work correctly. I would assemble an array of just valid field values and then use that array as input to Math.max().
Copy link to clipboard
Copied
Sir,
Your Math.max() method didnot worked , i am asking for pdf form javascripting, well i got something for pdf forms but this is not working.... since you are an expert, could you plz help me with the following script and edit the below script so that this works. My purpose is to find the greatest number among the 28 numbers populated by user my text field is name as DP.1, DP.2 ........ DP.28 and it should ignore the empty fields
// custom calculation script for "Min"
var minValid = false;
var min = 0;
for (var i=0; i>=4; i++)
{
var val = parseInt(this.getField("DP." + i).value);
if (!isNaN(val)) // we have a valid number
{ if (minValid == true)
{ // this is the first valid value
min = val;
minValid = false;
}
else
{
min = Math.min(min, val);
}
}
}
if (minValid)
event.value = min;
else
event.value = "";
plz edit this javascript to suit my needs
Copy link to clipboard
Copied
You cannot copy&paste your way to a working JavaScript solution. You need to understand what the script is doing in order to modify it based on your needs. There is a serious problem with the for loop in your code, which causes the loop to never actually be executed. Take a look at this line:
for (var i=0; i>=4; i++)
Now check the syntax for the for loop in JavaScript: Loops and iteration - JavaScript | MDN
As you can see, the second part of the for loop syntax (the "condition") is used to find out for how long the for loop gets executed, when that condition returns "false", the for loop stops. You start the loop with a value of 0, but you test for a value equal or greater than 4 - that will always fail for your initial value of 0, so the loop never gets executed. Also, your field names start with an index of 1, not 0, so even if the loop would execute, the first call to getField() would return a null value and your attempt to get the "value" property would result in an exception which you are not catching.
To get things started, use the following code to run your for loop:
for (var i=1; i<=28; i++)
And of course, you have to change the Math.min() call to a Math.max() to get the maximum.
Do yourself a favor and learn enough JavaScript so that you at least know what you are copying and pasting. As Try67 suggested, we are not here to do your work, we can point you in the right direction and look for errors in your code, but you will have to do some of the work.
Copy link to clipboard
Copied
I also want you to just point out the errors in the code so that it goes in the right direction,, as i have no knowledge of JavaScripting,
i did the above changes as you suggested but it prints nothings, field remains blank......
Copy link to clipboard
Copied
Sir,
As per your suggestion i changed the Fields name to DP.0, DP.1 ..... DP.27
below is the script i am trying to execute after incorporating the changes suggested by you, but it is not printing anything.....
var maxValid = false;
var max = 0;
for (var i=0; i<=28; i++)
{
var val = parseInt(this.getField("DP." + i).value);
if (!isNaN(val)) // we have a valid number
{ if (maxValid == true)
{ // this is the first valid value
max = val;
maxValid = false;
}
else
{
max = Math.max(max, val);
}
}
}
if (maxValid)
event.value = max;
else
event.value = "";
Copy link to clipboard
Copied
If you have fields named from 0 to 27, you cannot run your loop from 0 to 28 - unless you have an exception handler. You need to limit the loop to run from 0 to 27. There is another problem with your code which I did not see last time around: You initialize maxValid to "false", but then you test for "true" inside your loop (and set it to "false" if it was true). You don't have a valid value yet on the first iteration, so you need to check for "false" and set the variable to "true" inside the "if" block.
Copy link to clipboard
Copied
Thank you sir your guideline help me to achieve wat i wanted. thank you very much ,
below is the script which worked for me based on your guidelines :
var maxValid = true;
var max = 0;
for (var i=0; i<=27; i++)
{
var val = parseInt(this.getField("DP." + i).value);
if (!isNaN(val)) // we have a valid number
{ if (maxValid == false)
{ // this is the first valid value
max = val;
maxValid = false;
}
else
{
max = Math.max(max, val);
}
}
}
if (maxValid)
event.value = max;
else
event.value = "";
Copy link to clipboard
Copied
Sir,
could you plz tell me a name of book which is good for pdf java scripts learning both beginner and expert level.
Copy link to clipboard
Copied
If you want a basic introduction just into Acrobat's JavaScript, try this: Beginning JavaScript for Adobe Acrobat
For anything above this basic level, you can use any book about JavaScript, but you have to keep in mind that most books are written for JavaScript in a browser environment. All Javascript implementations have the "Core" language in common, but then add application specific extensions to that core. In Acrobat, these extensions are about PDF files, form fields, annotations and other things that you can create or manipulate with Acrobat's JavaScript. In the browser, you are dealing with HTML, HTTP connections, browser windows and more. As soon as your resource starts to move away from the core JavaScript language, you need to stop reading, because you would not be able to use any of that information in Acrobat. The definitive document about Acrobat's Javascript is the Acrobat JavaScript API reference:
Acrobat DC SDK Documentation - JavaScript for Acrobat API Reference
The Acrobat API also contains a document that describes how you can use JavaScript in Acrobat:
Acrobat DC SDK Documentation - Developing Acrobat Applications using JavaScript
I would recommend that you start with "Beginning JavaScript for Adobe Acrobat", then read the "Developing Acrobat Applications Using JavaScript" document, and then move on to a more detailed document about the JavaScript core language, followed by the JavaScript for Acrobat API Reference (which you do not have to read page by page, but you should get an idea about what objects exist, and what you can do with them).
Copy link to clipboard
Copied
I've provided this information in their other thread (Re: I want a javascript for adding minutes (:00 format) and the should be in total (:00000 format) ), but they seem to want us to do their work for them...
Copy link to clipboard
Copied
wat are you talking about sir,, you provided me with Math.max() method.. which didnot worked for me..,,, i found the above script which also didnot worked for me.. so i just want you to edit it since you people are experts,, and this forum is for helping .. isn't it...
Copy link to clipboard
Copied
Yes, it's for helping, not for doing someone else's work for them.
Copy link to clipboard
Copied
You are putting this in the "Custom JavaScript calculation" option. The operative word is "custom". You need to customize the script to your particular form. This usually means adjusting field names to match with your form fields and possibly the number of form fields. Also most arrays in JavaScript use zero base numbering, so the first element is zero and the last element is the number of items less 1. This zero base numbering applies to many items in JavaScript also many computer languages use this approach in numbering arrays.. As Karl has suggested you need to learn some basic coding and just not guess at what you think it should be.
Copy link to clipboard
Copied
One possible script for the custom calculation option is:
// array of field names to find the max/min value;
var aNames = new Array(
"Number.0", "Number.1", "Number.2", "Number.3",
"Number.4", "Number.5", "Number.6", "Number.7",
"Number.8", "Number.9", "Number.10", "Number.11",
"Number.12", "Number.13", "Number.14", "Number.15",
"Number.16", "Number.17", "Number.18", "Number.19",
"Number.20", "Number.21", "Number.22", "Number.23",
"Number.24", "Number.25", "Number.26", "Number.27"
);
// array of values to be processed;
var aValues = new Array();
var oTempField
// add field values to array of values to be processed;
for(var i =0; i < aNames.length; i++)
{
oTempField = this.getField(aNames);
if(oTempField == null)
{
app.alert("Error accessing field " + aNames + "\nPlease check the existence of field.", 1, 0);
}
if(oTempField != null && oTempField.valueAsString != "" && isNaN(oTempField.valueAsString) == false)
{
aValues.push(oTempField.value);
} // end valid number;
} // end field name loop;
// clear the result;
event.value = "";
// determine the maximum value there are values in the aValues array;
if(aValues.length > 0)
{;
// apply Math.max to each element in the array of values
event.value = Math.max.apply(null, aValues);
}
Another approach could include the filter method of an array processing each pair of value in the array;

