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

Acrobat 9 Issue: Show alert when field is empty

ppachen
Registered: Jan 12 2007
Posts: 6
Answered

The following code works perfectly in Acrobat 8 Professional but is giving me issues in Acrobat 9 Reader. When I run it in 9 Reader, the alert window does not close when I click OK or press Enter . The only way I can get it to close is by holding down Enter. The code is set to run a javascript when the OnBlur action occurs.

Any idea why this is happening?

f = getField(event.target.name);
if (f.value.length == 0) {
f.setFocus();
app.alert("This field is required. Please enter number.");
}

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
A setFocus() action is a bit of a tricky thing to put into an OnBlur event. You have real potential for an infinate loop and that's exactly what's happening. The code sets focus back into the field and then displaying the alert box, which removes focus from the field, queing up another OnBlur event.

In Acrobat 8 the timing is a bit different so that the focus is set back to the field after the alert returns. And that of course is the solution, move the setFocus() after the alert.

There are a couple of other things that are not quite right with this script. First, the getField line is redundant. The field is already in the event object, i.e, event.target. The script should look like this.
if (event.target.value.length == 0) {app.alert("This field is required. Please enter number.");event.target.setFocus();}

Second, the script itself, while it works, falls a ways outside of best practices. Field validation should be done in two places, in the Validate event and at submission time.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

ppachen
Registered: Jan 12 2007
Posts: 6
Thanks, that seemed to work under the onBlur action. Out of curiousity, do I need to alter the code in order to place it as a validation script? I used the code as-is and it didn't seem to do anything.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Yes, the code needs to be modified to work in a validation script. In a validate event the new field value is stored in "event.value". This is covered in the Acrobat JavaScript Reference, the Acrobat JavaScript Guide, there's an article on it in the tutorials on this site, and there are video tutorials, scirpt samples, and demo files that cover this issue ad-nauseum at www.pdfscripting.com. Take a look at any of these.

The validation script is only called when data is entered into the field. That's why null testing (i.e. empty testing) is done at submit time.


Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script