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

Check box to toggle validation script

Queue
Registered: Apr 27 2009
Posts: 22

Howdy all,

I'm working on an Acroform (Acrobat 8.1.2, WinXP Pro) that needs to have a simple feature: a custom validation script that can be toggled on or off through the use of a check box.

The checkbox ("CheckBox1") should point at the text box ("TextBox1"). My JavaScript skills are largely zero, unfortunately. I do have the Adobe JavaScript Scripting Reference pdf, the API guide, and I've searched through a lot of the old threads here at the AUC forums. I've found the Field/Validate control on page 337 in the scripting reference but there's no example on how to use it.

So that being said, I need the following:

1.) A Checkbox JS that toggles the Text Box Custom Validation Script on and off, something like this:

if (this.rawValue==1) TextBox1.rawValue= ;

2.) A Custom Validation Script for my Text Box that would make sure the field value is formatted in a Date format ("MM/DD/YYYY"). I have a few threads I'm reading through since this is a common problem.

Thanks in advance.

My Product Information:
Acrobat Pro 8.1.2, Windows
Queue
Registered: Apr 27 2009
Posts: 22
Here's some code I've bashed together as a starting point:

// Check box toggle to make field required (function (){ var Toggle = this.getField("TextBox1"); Toggle.required = true; }) ();

When I put this into my [u]CheckBox1[/u] check box, the JavaScript console comes back with this error:

"JavaScript Console" wrote:
Toggle has no properties
5:Field:Mouse Up
So... I'm obviously missing something. :)
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
The Mozilla documentation for [url=https://developer.mozilla.org/En/JavaScript]JavaScript 1.5[/url].
[url=http://www.adobe.com/devnet/acrobat/javascript.php]JavaScript for Acrobat[/url]

You can create a document level function that is passed a field name and the function will toggle the 'required' property for the passed field name.
// document level function to toggle required property of a fieldfunction ToggleRequired(sFieldName) {// toggle a field's 'required' property between true and falsevar oField = this.getField(sFieldName);// toggle a field's 'required' property between true and falseoField.required = !oField.requiredreturn;} // end ToggleRequired// end document level function

You can then add a call to the function providing the field name for the field you want the 'required' property toggled between required and not required.
// mouse up action for check box or buttonToggleRequired("TextBox1");

Now if you need to check that every 'requried' field is completed, you will need to create your own script as this feature applies to form submitted to a scriptted web pabe, but many write their own scripts for use prior to submission because the web page will trhow a generic error message.

George Kaiser

Queue
Registered: Apr 27 2009
Posts: 22
Thanks for the post, gkaiseril! However, after I've made the document-level function (called "ToggleRequired"), and then put the code in my "CheckBox1" checkbox, it doesn't seem to do anything.

Here's what I entered:
// document level function to toggle required property of a fieldfunction ToggleRequired(TextBox1) {// toggle a field's 'required' property between true and falsevar oField = this.getField(TextBox1);// toggle a field's 'required' property between true and falseoField.required = !oField.requiredreturn;} // end ToggleRequired// end document level function

In my "CheckBox1" check box, I put in the following code:
// mouse up action for check box or buttonToggleRequired("TextBox1");

The Java Console shows no error, and yet I can click into the target field "TextBox1" and click out again without error. I can also click into the field "TextBox1", type something, then delete it and click out again without error. So unfortunately it's not working yet. Where did I drop the ball?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
What are you expecting to happen?

The function changes a property of the a field and this usually will not be seen. You may have to tab through a field or two to get the 'required' highlight to catch up to the change, but you will only see this highlighting if you have set your application's preferences.

Have you checked the field's 'required' check box in the pop-up properties for that field?

Try the following code for the "mouse up" action for the check box:
console.show();console.clear();console.println(this.getField("TextBox1").required);// mouse up action for check box or buttonToggleRequired("TextBox1");console.println(this.getField("TextBox1").required);

George Kaiser

Queue
Registered: Apr 27 2009
Posts: 22
That code does printout to the console, so the check box is firing. :)

Right now on this sample PDF I have only a single checkbox ("CheckBox1") and a text box ("TextBox1"). There are no other fields to tab through, the focus just tabs back and forth.

Ideally, I'd like to have a checkbox that, when checked will then require that the user fill in a date in before tabbing/clicking off of the text box. When the checkbox is unchecked, then the text field behaves like a normal field.
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4308
Now you have to write all the other needed code and condition tests.

George Kaiser

Queue
Registered: Apr 27 2009
Posts: 22
Alright, I'll get cracking. :D

Thanks for your help -- I will add to this thread as I come back to this problem.