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

Javascript for mixing required text fields and radio buttons

zeborn
Registered: Jan 13 2009
Posts: 6

I have developed a form with 2 required text fields and 8 required radio button fields. Through the help of this website I have the radio buttons working to send an alert if the fields are not completed but can't for the life of me figure out how to do the same with the text fields. I thought appending the if statement would work (after naming the text fields activity1 and activity2 and adding them to the script) but I can't get it to. Below is my script, any help would be much appreciated. I'm sure it's probably just another line or so but have been looking at this for a few days now and my eyes are beginning to cross. Thanks!

var aReqFields = new Array("activity3", "activity4", "activity5", "activity6", "activity7", "activity8", "activity9", "activity10");

var fld;
var nActivityCount = 0; // clear count
for(var i = 0;i < aReqFields.length; i++) {

fld = this.getField(aReqFields[i]);

if(fld.value != "Off" )
nActivityCount++;
}

if(nActivityCount > 7)
Print(true);
else
app.alert({
cMsg:"You have omitted one or more required fields. Click the CANCEL button to cancel printing. Complete all required fields and then click PRINT

My Product Information:
Acrobat Pro 8.0, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can create variables to store the result of multiple test and then test the logical value of all those test results and take the appropriate action.

Form your starting script, one could modify it as follows:

// test for required radio buttonsvar bRadioTest = false; // assume test failed// radio button fields to testvar aReqFields = new Array("activity3", "activity4", "activity5", "activity6", "activity7", "activity8", "activity9", "activity10"); var fld;var nActivityCount = 0; // clear countfor(var i = 0;i < aReqFields.length; i++) { fld = this.getField(aReqFields[i]); if(fld.value != "Off"  )nActivityCount++;}// see if radio button test passedif(nActivityCount > 7)bRadioTest = true; // test text fieldsvar bTextTest = true; // assume passedaReqFields = new Array("Text1", "Text2")for(var i = 0;i < aReqFields.length; i++) {fld = this.getField(aReqFields[i]);// if field is empty string test failsif(fld.value.toString() == "" )bTextTest = bTextTest & false; // failed test} // end loop // see if both test were passedif((bRadioTest == ture) & (bTextTest == true))Print(true);elseapp.alert({cMsg:"You have omitted one or more required fields. Click the CANCEL button to cancel printing. Complete all required fields and then click PRINT"});

George Kaiser

zeborn
Registered: Jan 13 2009
Posts: 6
Thanks again for your help on this. I still can't get it to work, tried the code and it is now printing even when no fields (text or radio buttons) are completed. I have the text fields first on my form and don't know if that makes a difference in the script. I tried changing it around but still couldn't get it to work. Any ideas?
zeborn
Registered: Jan 13 2009
Posts: 6
I got it working, changed text boxes to activity1, activity2 and changed my activitycount to >9. Changed my if statement to:if((fld.value != "Off") && (fld.value !== ""))
nActivityCount++;

This seems to work as it is finally counting all 10 activities. Thanks again for all your help!
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
Here is the code with a modification for the radio buttons either being "Yes" or "No" and some debugging code:

console.show();console.clear(); // test for required radio buttonsvar bRadioTest = false; // assume test failed// radio button fields to testvar aReqFields = new Array("activity3", "activity4", "activity5", "activity6", "activity7", "activity8", "activity9", "activity10"); var fld;var bActivity = true; // clear countfor(var i = 0;i < aReqFields.length; i++) { fld = this.getField(aReqFields[i]);console.println(aReqFields[i] + ".value = " + fld.value);// test for radio button to be Off or Noif(fld.value == "Off" | fld.value == "No")bActivity = false;}console.println("bActivity: " + (bActivity == true) ); // test text fieldsvar bTextTest = true; // assume passedaReqFields = new Array("Text1", "Text2")for(var i = 0;i < aReqFields.length; i++) {fld = this.getField(aReqFields[i]);console.println(aReqFields[i] + ".value = " + fld.value);// if field is empty string test failsif(fld.value.toString() == "" )bTextTest = false; // failed test} // end loopconsole.println("bTextTest: " + (bTextTest == true) );// see if both test were passedconsole.println("(bActivity == true) & (bTextTest == true): " + ((bActivity == true) & (bTextTest == true) == 1) );if((bActivity == true) & (bTextTest == true))Print(true);elseapp.alert({cMsg:"You have omitted one or more required fields. Click the CANCEL button to cancel printing. Complete all required fields and then click PRINT"});

George Kaiser