These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

javascript forms calculation

klickacro
Registered: Feb 14 2011
Posts: 4
Answered

How could I get a javascript (or a simplified form calculation)to get working with the following formula (which is the Friedewald formula for LDL cholesterol)?
LDL = TC - HDL - TG/5.0
 
LDL(say Form "D") = TC(say Form "A") - HDL(say Form "B") - TG/5.0(say Form "C")
 
Thank you for your help
 
Paolo

Paolo

My Product Information:
Acrobat Pro Extended 9.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
Accepted Answer
Simplified Field Notation:

A - B - C/5




JavaScript:

event.value = getField("A").value - getField("B").value - getField("C").value / 5;


assuming the fields are numeric.


klickacro
Registered: Feb 14 2011
Posts: 4
Hi George,
Thank you very much for your quick reply. The javascript works fine, it is perfect, it is the flag of the "kiss" principle.

Instead, the simplified form calculation is giving me a message on the console: "missing ; before statement
1:Field:Calculate"

the following is exactly what I wrote in the script: "Colesterolo tot-Colesterolo HDL-Trigliceridi/5"
(of course these are the names of the forms)
Thank you anyway for your help!

Paolo


Paolo

klickacro
Registered: Feb 14 2011
Posts: 4
Hi George,

I've found the root of my error in your reply to a previous post (3. Feb 9, 2011 9:39 AM in response to: z71hunter). I've changed the forms' names whithout leaving spaces inside them.

Thank you again for your very appreciated help!

Paolo


Paolo

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You can have spaces or dots in your field names with the simplified field notation, but you need to use the JavaScript escape character, "\", before the space or dot in your script.

So if you have a field named "Sales Tax Rate" and a field called "Subtotal" and the sales tax is computed by the "Sales Tax Rate" times the "Subtotal" your simplified field notation could be:

Sales\ Tax\ Rate * Subtotal

For the custom calculation script could be:

event.value = this.getField("Sales Tax Rate").value * this.getField("Subtotal").value;

For hierarchical field names whose levels are separated by the dot, ".", character, you can place the JS escape character, "\", before the dot character and use the simplified field notation.

George Kaiser

klickacro
Registered: Feb 14 2011
Posts: 4
Salve gkaiseril,

thank you for your detailed an extended explanation of the rules, I'm sure it will be very useful for my future activity.
Paolo

Paolo

RD3
Registered: Feb 22 2011
Posts: 2
Hi ther,
After reading for many hours i can see that i still don't understand many things. Sorry if my question is obvious but i would like to do a simple quiz exercise.
I have a form with empty field and the student has to fill the fields with the correct word.
How can i validate the field?
For exemple the empty field should be populate with the word "cow"
If the student types another animal name a message should appear saying "Wrong answer"
If the student writes cow then nothing happens.
Badly written:
If the value of the field is = to "cow" then display message ifnot do nothing
Thanks you for indicating me how to start.

More i learn, less i know.

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
If you are going to test text strings, then the case of the answer can be an issue. If you will accept any case, then you need to make sure all values being compared have the same case. Or you can force the student to answer the question with the correct name and case. You can force an answer to either all upper case or lower case letters using JavaScirpt's 'toUpperCase()' or 'toLowerCase()' methods.

When do you want to apply this test?

You can apply the test after the student has answered all of the questions or when the student completes the each item.

If it is when the student completes the item, how do you want to handle a no answer is provided for the question?

How do you want to handle the ability to skip over a question?

It is possible to lock out any following questions until a question is answered.


George Kaiser

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
In the Validate event of the field, you could do something like:

  1. // Custom Validate script for text field
  2. (function () {
  3.  
  4. // Don't do anything if field is blank
  5. if (!event.value) return;
  6.  
  7. // This is the correct answer
  8. var correct_answer = "cow";
  9.  
  10. // This is what the user entered, converted to lower case
  11. var answer = event.value.toLowerCase();
  12.  
  13. // Compare answer to correct answer and alert if they don't match
  14. if (answer !== correct_answer) {
  15.  
  16. app.alert("Your message goes here.", 1);
  17.  
  18. // Optionally, reject the answer
  19. event.rc = false;
  20. }
  21.  
  22. })();
You might want to add code to remove any leading or trailing spaces, but this should get you started.
RD3
Registered: Feb 22 2011
Posts: 2
Thank you so much from Barcelona!!
It works perfectly as i wanted.
wouaaaa! i can now continue investigating!
Thank you again!

More i learn, less i know.