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

Common custom function

karl_s
Registered: Jun 8 2009
Posts: 4
Answered

I have a validation that I want to perform on 15 fields in a form. The validation is basically the same, with the only difference being which 3 fields are used for the validation. While I could put the script in for each field, it seems a better solution would be to call a common function, passing the value of the 3 fields I need, (OK, I just learned it should be 2 fields, and event.value, thank you , again, forumers!) My problem, though, is I don't know where to put this common function so it is stored in the PDF and called by my validation routine. I looked around, but must not be searching with the right terms. Can someone point me in the right direction?

Karl

My Product Information:
Acrobat Pro 9.3.1, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can put the function in the "Document Level JavaScript Functions" ("Advance => JavaScript => Document JavaScripts...").[url=http://www.acrobatusers.com/tutorials/2006/where_js_goes]So where does JavaScript live in Acrobat?[/url] by Thom Parker
[url=http://www.acrobatusers.com/tutorials/2007/07/js_document_scripts]Entering Document Scripts[/url] by Thom Parker

function Sum100(nValue1, nValue2, nValue3) {var bValid = true; // assume test passedif ( 100 < Number(nValue1) + Number(nValue2) + Number(nVvalue3) ){app.alert("The total value can not be more than 100.");bValid = false; // mark as failed}return bValid; // return results of test} // end of function Sum100

You could then call it as a custom validation with:
var nKCI = this.getField("RegistrationKCI").value;var nParticipant = this.getField("RegistrationParticipant").value;event.rc = Sum100(event.value, nKCI, nParticipant);if (event.rc = false) {// additional code for a failed result}

George Kaiser

karl_s
Registered: Jun 8 2009
Posts: 4
You took all the fun out by anticipating what I was doing this on! Thank you!

Actually, there were a couple minor problems.

For the Document function, I had to put parenthesis around the equation to add up the numbers prior to checking their value against 100, and remove an extra v in nValue3. I ended up with:
function Sum100(nValue1, nValue2, nValue3){var bValid = "true"; // assume test passedif ( 100 < (Number(nValue1) + Number(nValue2) + Number(nValue3)) ){app.alert("The total value can not be more than 100.");bValid = "false"; // mark as failed} return bValid; // return results of test} // end of function Sum100

And in the field's validation, I had to use 2 equal signs for the conditional. I now have:
var nKCI = this.getField("RegistrationKCI").value;var nParticipant = this.getField("RegistrationParticipant").value; var bTested = true bTested = Sum100(event.value, nKCI, nParticipant); if (bTested == "false"){//Set the focus back to the field jsut changedevent.target.setFocus();}

For some reason non value was being saved to the field when I used event.rc, so I swapped it out to a different variable. so I still had the opportunity to work my way thru and understand the logic better!

Ultimately, since all fields were being handled the same, I pared this down to:
Document script:
function Sum100(nValue1, nValue2, nValue3){var bValid = "true"; // assume test passedif ( 100 < (Number(nValue1) + Number(nValue2) + Number(nValue3)) ){app.alert("These fields represent a percent.  The total value can not be more than 100.");event.target.setFocus(); bValid = "false"; // mark as failed} return bValid; // return results of test} // end of function Sum100

And Field code:
var nKCI = this.getField("RegistrationKCI").value;var nParticipant = this.getField("RegistrationParticipant").value; var bTested = Sum100(event.value, nKCI, nParticipant);

I probably could have not returned any value and saved a little more, but I wanted to keep that in for any future example I may need.

Thank you, gkaiseril, for the assistance!

Karl