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

help with code

Community Expert ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

if(this.getField("field").checkThisBox(0, event.value=="4/8"));

How do I make it check the checkbox even if values are "5/8","6/8","7/8" and "8/8"?

TOPICS
Acrobat SDK and JavaScript

Views

1.2K

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 2 Correct answers

Community Expert , Jul 28, 2020 Jul 28, 2020

So here's what worked for me as custom calculation script of field2

 

var f = this.getField("checkbox");

var s = this.getField("field1").value +"/8";

event.value = s;

if (event.value) {

if ( (s == "4/8")  || ( s == "5/8")  || ( s =="6/8") || ( s =="7/8") || ( s == "8/8") ) {f.checkThisBox(0, true)}
if ( (s == "1/8") || (s == "2/8") || (s == "3/8") ) {f.checkThisBox(0, false)}

}

Votes

Translate

Translate
Community Expert , Jul 28, 2020 Jul 28, 2020

You can combine both functions into a single calculation script, like this:

var v1 = this.getField("field1").valueAsString;
var f = this.getField("checkbox");
if (v1 == "4" || v1 == "5" || v1=="6" || v1=="7" || v1=="8") {
	f.checkThisBox(0, true);
} else f.checkThisBox(0, false);
event.value = v1 + "/8";

Votes

Translate

Translate
Community Expert ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

I'm sure there's a variety of different ways to do it with javascript, but you can easily get the job done like this:

 

 

var s = event.target.value;
var f = this.getField("checkbox");
if ( (s == "4/8")  || ( s == "5/8")  || ( s =="6/8") || ( s =="7/8") ||  ( s == "8/8") ) {f.checkThisBox(0, true)}
else f.checkThisBox(0, false);

 

 

Also make the checkbox read-only so the users don't mess with its check /unchecked status if they try to click on it.

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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

Hi, thanks for your help.Code is working only if I enter values manually it doesn't work when other code change values in that field. I put code in validate of my textfield?

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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

I apologize. I am using it as a custom calculation script of the field where users will enter the values, not a validation script.

 

Were you originally asking about a validation script instead?

 

 

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 ,
Jul 27, 2020 Jul 27, 2020

Copy link to clipboard

Copied

I forgot to ask what other code are you using to change the values in that field?

 

Unless I missunderstood your quesiotn completely , I believe you didn't mention about using other code to interact with that field in your initial inquiry.

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

sry about that i should have mention that I'm using another code as custom calculation,this is code I use

event.value = Number(this.getField("field1").valueAsString) + "/8";

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

I don't want to sound a little slow here, but I am a bit lost with your workflow.  😞

 

I think I have an idea but is better if you briefly explain what is the workflow in your form, like for example, where does the user enter these numbers? is it in another text field? 

 

If yes, does the second script that you shared is a format script which adds the fractional part to whatever number the user inputs?  

 

And then another field grabs this value and if it doesn't match  these strings 4/8, 5/8, 6/8, 7/8   the checkbox remains uncheck?

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

It goes like this: user input value in dialog box which then write that value to field1, then field2 have custom calculation script if value in field1 is 1 it writes 1/8 in it's own field(field2) if value is 2 then it writes 2/8...etc

now what I need is if value in field2 is 1/8,2/8 or 3/8 checkbox is unchecked if values are 4/8,5/8,6/8,7/8 or 8/8 checkbox need to be checked.

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

You need to use calculation scripts for that, not validation, and to make sure that the fields calculation order is correct.

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

That code is in custom calculation script i fixed my error in post above.thx

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

So here's what worked for me as custom calculation script of field2

 

var f = this.getField("checkbox");

var s = this.getField("field1").value +"/8";

event.value = s;

if (event.value) {

if ( (s == "4/8")  || ( s == "5/8")  || ( s =="6/8") || ( s =="7/8") || ( s == "8/8") ) {f.checkThisBox(0, true)}
if ( (s == "1/8") || (s == "2/8") || (s == "3/8") ) {f.checkThisBox(0, false)}

}

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

You can combine both functions into a single calculation script, like this:

var v1 = this.getField("field1").valueAsString;
var f = this.getField("checkbox");
if (v1 == "4" || v1 == "5" || v1=="6" || v1=="7" || v1=="8") {
	f.checkThisBox(0, true);
} else f.checkThisBox(0, false);
event.value = v1 + "/8";

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

First of all guys I really appreciate yout help and sry I'm PITA.

Ok now on codes:

ls_rbls your code only check checkbox if value is 4/8

EDIT: also need to enter 5,6,7,8/8 twice to check it.

 

try67  your code also only  check checkbox if value is 4 and for values 5,6,7 and 8 I need to enter them twice cuz first time it actually uncheck checkbox and second time I enter value it check it.

 

EDIT2: In both codes only value of 4 (4/8) is working as intended.

Would it be easier if instead of checkbox it's radio button?

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

That doesn't make sense. Did you edit the code in any way? Can you share the actual file with us?

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

LATEST

I searched all my fields and there was validation script in one of the field that was interfering with the codes,

Both codes work fine now, I apologize it was my mistake. Thank you guys for helping me.

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