Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Value comparison must return greater value returns no value 2

Status
Not open for further replies.
Sep 12, 2007
143
US
Hi All, I'm trying to write a very simple script that will compare 2 values and return the greater of the two in a PDF form. So far, nothing I try will return a value.

It is a sales commission worksheet, where MinCommDue is a manually entered value that may or may not be populated (but is for my testing).
AllComs is a hidden field that is the sum of 4 other fields.

I need to compare the values and place whichever is greater in a different field.

I am very inexperienced in JS, any help would be greatly appreciated.

Here is the script:

var a=this.getField("MinCommDue");
var b=this.getField("AllComs");
if ((a.value >= b.value)){
event.value = a
} else {
if ((a.value <= b.value)) {
event.value = b
}
}
 
Ideally yes, the MinCommDue field could be left blank however. Otherwise, there should never be anything but numeric data in those fields.
 
I'm not sure about the answer, but here are some checkpoints:

- GetField returns the correct field
- a.value and b.value contain the expected values
- event.value should be a or b and not a.value or b.value

Cheers,
Dian
 
Diancecht, that's how it is written, but still not returning any value. Should a.value & b.value be written as a & b in my fi and else statements?

cLFlaVA, because I am too inexperienced to know how :) Tell how and I'll try it!
 
have you tried it yourself yet? there are examples in the link i provided above. something like

[tt]event.value = Math.max( a, b );[/tt]

what is "event"? if you are referencing / handling an event firing, i don't believe "value" is a legitimate property.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
What about this? Just coming up with it real fast..

Code:
var a = parseFloat(document.getElementById("MinCommDue"));
var b = parseFloat(document.getElementById("AllComs"));

if(!isNaN(a) && !isNaN(b)) {
   event.value = Math.max(a, b);
}else {
    // One of these was not a number
    event.value = "0";
}
 
cLFlaVA, I'm not sure why "event" is used, it works well in Adobe, and I have changed my script to:

var a = this.getField("MinCommDue");
var b = this.getField("AllComs");
event.value = Math.max(a.value,b.value);

This seems to be working exactly as needed, thank you very much!
 
MikeyJudd, I'll run that one through too, just to see if it works!
 
MikeyJudd, tried that and no value was returned, maybe it has to do with scripting within Acrobat? As I said, very green on JS! The simple 3 line script works though :) The only odd thing is that despite the formatting of each field as numeric, 2 decimal places, the script adds up what the fields would be if there were 3 decimal places and then rounds up to the next digit, causing it to be a penny off, which I can live with!
 
If any of these fields are entered by users, you should always take into account user error. So doing a safe guard to make sure they entered correct data is a good practice to have. Which is why I had you do that check :eek:)
 
Haha, like I said I wrote it fast. You're right, you do need the ".value" on the getElementById call.
 
Code:
var a = parseFloat(this.getField("MinCommDue").value);
var b = parseFloat(this.getField("AllComs").value);

if(!isNaN(a) && !isNaN(b)) {
   event.value = Math.max(a, b);
}else {
    // One of these was not a number
    event.value = "0";
}
 
MikeyJudd, I am unsure how I would "check" the value input. The data will be entered by a user who now fills in the form by hand. I think I understand that the NaN is checking that the input is numeric, am I on the right track? And NOW your script works!

Since both scripts work, and because I am so inexperienced, which script would be more prudent to utilize?

Thanks, Dawn!
 
Yes, the "isNaN" checks to make sure the value was parsed correctly to a float.

As a little FYI, "NaN" means "Not a Number".

As for which script to use, that is up to you. I personally would recommend the check I do. You mentioned that the data is entered by hand and humans make mistakes. Trust me, seen my share.

You could even extend your code to inform the user they made a mistake, such as:

Code:
var a = parseFloat(this.getField("MinCommDue").value);
var b = parseFloat(this.getField("AllComs").value);

if(!isNaN(a) && !isNaN(b)) {
   event.value = Math.max(a, b);
}else {
    // One of these was not a number
    event.value = "0";

    //Alert the user they entered the wrong data. This is assuming 'a' is the user input field
    alert("Number entered is not a valid number: " + a);
}
 
MikeyJudd, excellent! Thanks again, I learn something new everytime I come here :)

Dawn
 
Uh-Oh, got a syntax error on that last line, do I need a { before the word "alert"? That cleared the syntax error. (I try to learn, not just copy & paste!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top