• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Need javascript to checkbox in PDF form based upon value not null in another text field

Community Beginner ,
Feb 24, 2020 Feb 24, 2020

Copy link to clipboard

Copied

Please help with javscript in text field custom calculation field in Adobe Acrobat Pro DC fillable form.

I have a NO and YES checkbox, with a TextField that will be NULL or have a text value.

  1. I want a check in the checkbox NO, if the text field is NULL
  2. I want a check in the checkbox YES, if the text field is NOT NULL

 

Thanks in Advance!

 

I saw this script on a user board below, but receive a syntax error at line 7, but don't know how to fix.

if((String(event.value) == "") || (event.value == ""))
{
this.getField("LB8No").value = "Off";
this.getField("LB8Yes").value = "Off";
}
else if(event.value <> "")
{
this.getField("LB8No").value = "Off";
this.getField("LB8Yes").value = "On";
}

TOPICS
Acrobat SDK and JavaScript

Views

17.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 24, 2020 Feb 24, 2020

if (event.value.toString().length > 0)

{this.getField("CHECKBOX").value = "yes";}
else

{this.getField("CHECKBOX").value = "no";}

 

Be sure that the 2 checkboxes have exactly the same name and a different export value ("yes" and "no" in this sample).

Beware that JavaScript is case sensitive ("Yes" is not "yes").

Votes

Translate

Translate
Community Expert ,
Feb 24, 2020 Feb 24, 2020

Copy link to clipboard

Copied

if (event.value.toString().length > 0)

{this.getField("CHECKBOX").value = "yes";}
else

{this.getField("CHECKBOX").value = "no";}

 

Be sure that the 2 checkboxes have exactly the same name and a different export value ("yes" and "no" in this sample).

Beware that JavaScript is case sensitive ("Yes" is not "yes").

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 24, 2020 Feb 24, 2020

Copy link to clipboard

Copied

@JR_Boulay - Thanks for your prompt reply! I'll try and confirm on this post if it worked out.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 24, 2020 Feb 24, 2020

Copy link to clipboard

Copied

@JR_Boulay - That worked! Thanks so much for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 24, 2020 Feb 24, 2020

Copy link to clipboard

Copied

@JR_Boulay, I have a follow up question....the script works when I manually enter data in the "textfield" it checks or unchecks the appropriate box.

However, it does not automatically check the boxes when the "textfield" is auto-populated with a data function from the datadase that pre-populates the text field. The text field is a string of alphanumeric chars. In this case does "(event.value.toString().length > 0)" need to be changed to something else to recognize that the field is not null because the form's textfield was pre-populated with mapped data to database field.  Thanks for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 09, 2020 Nov 09, 2020

Copy link to clipboard

Copied

LATEST

Thank you JR,

I modified your answer to help me as a solution for my problem which was to have check boxes checked or not checked, based on a text field being not null.  This is an Acrobat X form.

My script is below and is added as a custom calculation:

if (event.value=="")
{this.getField("DealerLicNumber").value = "No";}
else
{this.getField("
DealerLicNumber").value = "Yes";}

 

If my text field is not blank, then two Yes checkboxes will be checked.  If text field is blank, then two No checkboxes will be checked.  Thanks to your answer to ds_mstar's question, this now works in my form.

 

If I manually check or uncheck one of the boxes, Javascript Debugger pops error messages.  Is there a way to account for this in the JavaScript?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 17, 2020 Aug 17, 2020

Copy link to clipboard

Copied

To loosely check if the variable is null, use a double equality operator(==). The double equality operator can not tell the difference between null and undefined, so it counts as same. So if you want to strict check, then don't use a double equality operator. It will lead to misconclusion.

The best way to check for a “null” value in JavaScript is to do a strict equality comparison of the value against the null keyword or Object.is() method and pass both values.

To check null in JavaScript, use triple equals operator(===) or Object.is() method. To find the difference between null and undefined, use the triple equality operator or Object.is() method.

If a checkbox is unchecked, it doesn't get sent, so setting it's value to 0 if it isn't checked isn't going to help - it will always return NULL.

There are two ways to fix this easily:

1) Assume a NULL in the PHP params means the checkbox is unchecked. If the checkbox doesn't always exist on the page, this could be problematic. By the sounds of it, there is a variable number of checkboxes, so this probably won't work.

2) Add a hidden input that has the same name as the checkbox with the value of 0 BEFORE the checkbox. If the checkbox is unchecked, the hidden field value will be used, if it is checked the checkbox value will be used.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2020 Aug 18, 2020

Copy link to clipboard

Copied

"However, it does not automatically check the boxes when the "textfield" is auto-populated with a data function from the datadase that pre-populates the text field."

In this case you should place the script as a "Validation" script instead of a "Calculation" script.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 18, 2020 Aug 18, 2020

Copy link to clipboard

Copied

"If a checkbox is unchecked, it doesn't get sent, so setting it's value to 0 if it isn't checked isn't going to help - it will always return NULL."

If a checkbox or a group of radio-buttons is not checked the returned value is "Off".

"null" means that the targeted form field does not exist.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines