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

Custom validation script with checkbox rules

Community Beginner ,
Oct 19, 2023 Oct 19, 2023

Hey guys, I have been learning quite a bit by asking my javascript questions here and listening to your responses. I really appreciate all the help I have gotten so far! I am at it yet again though, and now I would like to expand my current validation script to add a condition.

 

I have a text box that is a calculated % derived from some other boxes, and I have it changing colors based off the value. The twist is that I would like it to use one value if a certain checkbox is checked, and a different value if a different checkbox is checked.

 

The current validation script I am using is:

 

var v = event.value;

if (v>.7649) {event.target.fillColor = color.green;}

if (v<.7650) {event.target.fillColor = color.red;}

 

How would I modify this so that it also checks to see if a checkbox is checked as a condition? and is it possible to do like an "else" statement to use a different value if that checkbox is NOT checked, or if a DIFFERENT checkbox is checked? either way...

 

FYI the checkboxes are all named the same (Checkbox1), but the output values are different so that way only one can be checked at any given time.

 

Thank you again for all your help and support!

 

TOPICS
Create PDFs , How to , JavaScript , PDF , PDF forms
1.6K
Translate
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
2 ACCEPTED SOLUTIONS
Community Beginner ,
Oct 20, 2023 Oct 20, 2023

So to incorporate the 2 different color change conditions, would something like this work? Sorry, I am not at my workstation currently...

 

var v = event.value;
var c = this.getField("Checkbox1").valueAsString;

 

if((c == "A" || c == "B") && v > .7649)
{event.target.fillColor = color.green;}
else if((c == "A" || c == "B") && v < .7650)
{event.target.fillColor = color.red;}

 

if(c == "C" || c == "D" || c == "E" || c == "F") && v > .8149)
{event.target.fillColor = color.green;}
else if((c == "C" || c == "D" || c == "E" || c == "F") && v < .8150)
{event.target.fillColor = color.red;}

View solution in original post

Translate
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 ,
Oct 20, 2023 Oct 20, 2023

If you use it in a field where you wish to change color as a calculation script, it should work.

Just fix small bug, you are missing parenthesis here: if((c == "C"

Also consider adding default color if no checkboxes are selected.

View solution in original post

Translate
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 ,
Oct 19, 2023 Oct 19, 2023

Let's say you have two checkboxes with export values 1 and 2, you can use something like this:

var v = event.value;
var c = this.getField("Checkbox1").valueAsString;
if(c == "1" && v > .7649)
{event.target.fillColor = color.green;}
else if(c == "2" && v<.7650) 
{event.target.fillColor = color.red;}

I would suggest you to use it as calculation script, validation will not work if checkbox is checked after value is changed in field.

Translate
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 ,
Oct 19, 2023 Oct 19, 2023

I like your suggestion, but I need to add some further information:

 

Currently, the 5 boxes I want to change color are using a script in calculation that divides two different input boxes. Can I just add script after that to make the color change work?

 

Also, I have 6 unique checkboxes (model numbers), 2 of which I want the color change to happen from green to red at ~77%, and if any of the other 4 are checked, I want the color change to happen at ~82%

 

Appreciate your help so far, I am definitely closer to my goal than I was! I am starting to understand the syntax much better lol

Translate
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 ,
Oct 19, 2023 Oct 19, 2023

Yes, you can add color change script after your script, or you can put it in a totally different field.

Let's say you have export values A, B, C, D, E, F you can add multiple checkboxes like this:

 

 

var v = event.value;
var c = this.getField("Checkbox1").valueAsString;

if((c == "A" || c == "B") && v > .7649)
{event.target.fillColor = color.green;}
else if((c == "C" || c == "D" || c == "E" || c == "F") && v<.7650) 
{event.target.fillColor = color.red;}

 

 

 

Translate
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 ,
Oct 20, 2023 Oct 20, 2023

So to incorporate the 2 different color change conditions, would something like this work? Sorry, I am not at my workstation currently...

 

var v = event.value;
var c = this.getField("Checkbox1").valueAsString;

 

if((c == "A" || c == "B") && v > .7649)
{event.target.fillColor = color.green;}
else if((c == "A" || c == "B") && v < .7650)
{event.target.fillColor = color.red;}

 

if(c == "C" || c == "D" || c == "E" || c == "F") && v > .8149)
{event.target.fillColor = color.green;}
else if((c == "C" || c == "D" || c == "E" || c == "F") && v < .8150)
{event.target.fillColor = color.red;}

Translate
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 ,
Oct 20, 2023 Oct 20, 2023

If you use it in a field where you wish to change color as a calculation script, it should work.

Just fix small bug, you are missing parenthesis here: if((c == "C"

Also consider adding default color if no checkboxes are selected.

Translate
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 ,
Oct 20, 2023 Oct 20, 2023

The first way worked great! Thank you! I would also like to add your suggestion of if there is no check. I tried to add an if statement so that if the checkbox1 value was "off", it would be transparent.

 

I tried:

if(c == "off")

{event.target.fillColor = color.transparent;}

 

but it didnt do anything... then I tried:

 

if(this.getField("Checkbox1").value = "off")
{event.target.fillColor = color.transparent;}

 

and it worked initially, but after I checked and unchecked, it stayed green or red (probably because its checking once only and not constantly).

 

What would be the correct statement? I feel like I am missing something obvious lol.

Translate
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 ,
Oct 20, 2023 Oct 20, 2023
LATEST

1. It's "Off" not "off".

if(c == "off")

{event.target.fillColor = color.transparent;}

 

2. For comparison, use double equals == and again it should be "Off" not "off".

if(this.getField("Checkbox1").value = "off")
{event.target.fillColor = color.transparent;}

Translate
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 ,
Oct 20, 2023 Oct 20, 2023

I was thinking about it and this made a little bit more sense...

 

var v = event.value;
var c = this.getField("Checkbox1").valueAsString;

 

if(((c == "A" || c == "B") && v > .7649) || ((c == "C" || c == "D" || c == "E" || c == "F") && v > .8149)))
{event.target.fillColor = color.green;}
else if(((c == "A" || c == "B") && v < .7650) || ((c == "C" || c == "D" || c == "E" || c == "F") && v < .8150)))
{event.target.fillColor = color.red;}

 

Translate
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