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

Required Check Boxes

Lady Di
Registered: Feb 18 2009
Posts: 86
Answered

I am currently working on a form containing check boxes that are required (either yes or no needs to be checked) and a dollar amount field that is required (it needs to have something in it even if it is 0). Then I have an e-mail button that checks for required fields that are not filled out. My problem occurs, when the script behind the button does not recognize the check boxes as required and it does not allow a 0 dollar amount. Below is the code that I am using to check for required fields. Could someone please help me modify this so that it will capture everything it is supposed to?

var i,oCurrFld,tVal=""; //clear all variables
var cMsg = "";
for (var i = 0; i

My Product Information:
Acrobat Pro 8.0, Windows
jimhealy
Team
Registered: Jan 2 2006
Posts: 146
For Checkboxes and Radios that are not selected, the value is typically "Off". So you need to add that to your checks. You may also want to change your value = " " to value.replace(/(^\s)|(\s$)/g, "") = "". You also need to probably mark your checkboxes and radio buttons as required via javascript b/c you can't usually do that on the properties page for those types of widgets.

Jim Healy
FormRouter, Inc.
Check out our FREE Advanced Acroform Toolset:
http://www.formrouter.com/tools

Jim Healy, Founder & CEO FormRouter Inc.
Chapter Leader AUG RTP NC
http://www.formrouter.com

Lady Di
Registered: Feb 18 2009
Posts: 86
I changed this line of script: if (value == ""||value == " ")
to read like this: if (value.replace(/(^\s)|(\s$)/g, "") = ""||value == " "||value == off)

Now, when I click the button this script is tied to, it doesn't highlight anything to identify the required fields. What have I done wrong?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
I would suggest somehting like:
var i,oCurrFld,tVal=""; //clear all variablesvar bFailed = false; // assume field passed testvar cMsg = "";for (var i = 0; i<this.numFields; i++){oCurrFld=this.getField(this.getNthFieldName(i));with (oCurrFld){// assume field passed testbFailed = false;fillColor = color.transparent;// test for required fields being completeif (type!="button" && required){if (value == ""||value == " "){bFailed = true;}// add test for check boxes or radio buttons being "Off"if(type == 'checkbox' | type == 'radiobutton'){bFailed = true;}// add additonal field type special test here                       }  // end if required } // end current field // test if field failed any testif(bFailed){// highlight field and set messagefillColor = ["RGB", 255/255, 255/255, 150/255];cMsg = cMsg + (name + " is required information. \n");} // end failed} // end field loopif (cMsg != "") {showMsg = "Please complete the following entries (boxes highlighted in " +"yellow) before printing this form: \n\r" + cMsgapp.alert(showMsg, 1);

George Kaiser

Lady Di
Registered: Feb 18 2009
Posts: 86
I inserted the code you suggested, however, I keep getting an error message stating: "missing ) after condition 22: at line 23." Then it highlights the line that reads: "bFailed = true;" right under where it says "if(type == 'checkbox' | type == 'radiobutton')".

I don't understand what it means. I added a parenthesis at the end of that line, but I still got the error message. What do I need to do?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
The posted code was missing the closing ')'.

George Kaiser

Lady Di
Registered: Feb 18 2009
Posts: 86
Thank you.

However, I'm afriad, I am still having a problem. There are actually three things that I am trying to accomplish with one button. My problem may be that I am trying to get one button to do too much. Of course, my problem could also be that I am a novice when it comes to scripting. Below is the complete code that I have on this button. The middle section of this script is the script that was posted here. I want the button to fill in a date field, check to make sure all required fields are filled in, and if all required fields are filled in, then generate an e-mail with the pdf form as an attachment. For some reason, only the first part of the script is working. When I click on my button, all it does is fill in the date. What do you suggest?

getField("ChangeRequestDate").value = util.printd("mm/dd/yyyy", new Date());
var i,oCurrFld,tVal=""; //clear all variables
var bFailed = false; // assume field passed test
var cMsg = "";
for (var i = 0; i
Lady Di
Registered: Feb 18 2009
Posts: 86
Okay, I got the above code to start working. However, I am now having another problem. I have a dollar amount field that simply needs to contain a number be it a 0, a negative number, or a positive number. This code recognizes the negative and positive numbers, however, it is not recognizing the 0. If I enter 0 in the dollar amount field, this code tells me that I need to fill in the field. How can I solve this issue?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
Numbers are not stored as a string of characters, but as a binary number, to test a number field for being a null, space or zero or to test the length of a number filed, one needs to convert the number to a string. This can be done using the "toString()" method or "valueAsString" property.

JavaScript tends to make a guess at the type of data is in a text field and will adjust between a character string and a number as JavaScript interprets the action being requested. For subtraction, multiplication and division a character string is treated as a number with a null field being treated like a zero. For addition a space will cause the '+' operator to act as the concatenation operator.

George Kaiser

jimhealy
Team
Registered: Jan 2 2006
Posts: 146
Going back to your post:
Quote:
I changed this line of script: if (value == ""||value == " ")
to read like this: if (value.replace(/(^\s)|(\s$)/g, "") = ""||value == " "||value == off)
The problem there was that you used value == off and you needed value == "Off". You also do not need the value == " " (the one with the space in it) as the replace I gave you removes leading and trailing spaces from the value.

As far as this code:
Quote:
// add test for check boxes or radio buttons being "Off"
if(type == "checkbox")
{
bFailed = true;
}
I don't see what you are doing there. You are always saying that the box is not checked. Get rid of that and go back to your original condition with the == "Off" thing fixed. As far as your problem with 0, you can cast a variable to a Number by using Number() and then you can do a numeric compare. You should be sure to check that the result is a Number though and then do the compare, like this:
var iNum = value;if(!isNaN(iNum) && iNum == 0)

Jim Healy
FormRouter, Inc.
Check out our FREE Advanced Acroform Toolset:
http://www.formrouter.com/tools

Jim Healy, Founder & CEO FormRouter Inc.
Chapter Leader AUG RTP NC
http://www.formrouter.com

Lady Di
Registered: Feb 18 2009
Posts: 86
Thank you very much for your assistance. I apologize for my ignorance on this subject, but I am very new to scripting. Please bear with me.

Where would I need to put this script?

I have custom format script on the dollar amount field that reads - AFNumber_Format(2, 0, 3, 0, "$", true). Does this script need to go after that somehow, or does it need to go somewhere in the script to check the fields?
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You can change the line:
             if (value == ""||value == " ")
to:
             if (valueAsString == ""||valueAsString == " ")
If you are using the "Format" tab options for "Number" and using the "Currency Symbol" option, then the value within Acrobat JavaScirpt is the number without the currency symbol and only the decimal point.

For the prior code I posted, I forgot to include the test for the 'Off' value. The line:
  if(type == 'checkbox' | type == 'radiobutton')
could read:
  // if the field is a check box OR radio button AND the value of the field is 'Off'if( (type == 'checkbox' | type == 'radiobutton') & value = 'Off")

George Kaiser