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

Calculation or Validation

prander
Registered: Nov 6 2008
Posts: 44
Answered

I’m currently working on a performance review in which one of the review categories assigns a score based on the hours absent. The maximum score is 5. The table is

Hours Absent Score

0-8 5
9-32 4
33-40 3
41-57 2
58-66 1
67 or higher 0

I’d like for the Score field to automatically calculate based on what is the user types in the "Hours Absent" field. Since that value is a range, what is the best way to create this, validation or calculation? And how would I write that script?

Thanks in advance

My Product Information:
Acrobat Pro 8.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
You can use either a Validation script for the "Hours Absent" field or Calculation script for the "Score" field. Assuming the form was created in Acrobat (as opposed to LiveCycle Designer), you could use something like the following as the custom Validation script for the "Hours Absent" field:

(function () { // Get this field's entry (event.value is a string)var sValue = event.value; // Get a reference to the score fieldvar f2 = getField("your_score_field"); // If the field is blank, set score field to blankif (sValue === "") {f2.value = "";return;} // Convert field entry to an integer numbervar nValue = Math.floor(+event.value); // Set score field value based on this field's valueif (nValue < 0) {f2.value = "";return;}if (nValue >=  0 && nValue <=  8) {f2.value = 5;return;}if (nValue >=  9 && nValue <= 32) {f2.value = 4;return;}if (nValue >= 33 && nValue <= 40) {f2.value = 3;return;}if (nValue >= 41 && nValue <= 57) {f2.value = 2;return;}if (nValue >= 58 && nValue <= 66) {f2.value = 1;return;}if (nValue >= 67) {f2.value = 0;return;} // This should never happen, but just in case...f2.value = ""; })();

This code assumes that the "Hours Absent" field has a numeric format category. Replace "your_score_field" with the actual name of your score field. You may want to do additional validation of the input, but you should get the idea. Notice that if the user enters a negative number, the code above just sets the score field to blank.

I use validation scripts when possible as it is more efficient and avoids a number of other issues that can crop up when using calculation scripts.

A custom calculation script would be similar, but have important differences. If you want to use a calculation script for the score field, post again.

George
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
You could also use the "On Blur" action to set the score field when the user leaves the field for the "Hours Absent" that would leave the validation option free so one could enter "0" for the "field in range" option.

Your script could be:

// select the true statementswitch(true) {// null or no entrycase (event.value.toString() == '') :this.getField("Score").value =  ''break; // => 0 and <= 8case (event.value <= 8) :this.getField("Score").value =  5;break; // > 8 and <=32case (event.value <= 32) :this.getField("Score").value =  4;break; // > 32 and <= 40case (event.value <= 40) :this.getField("Score").value =  3;break; // > 40 and <= 57case (event.value <= 57) :this.getField("Score").value =  2;break; // > 57 and <= 66case (event.value <= 66) :this.getField("Score").value =  1;break; // all others > 67default:this.getField("Score").value =  0;break;} // end switch

George Kaiser