Skip to main content
Participating Frequently
October 19, 2023
Answered

Custom validation script with checkbox rules

  • October 19, 2023
  • 1 reply
  • 2281 views

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!

 

This topic has been closed for replies.
Correct answer Nesa Nurani

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;}


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.

1 reply

Nesa Nurani
Community Expert
Community Expert
October 20, 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.

Participating Frequently
October 20, 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

Nesa Nurani
Community Expert
Community Expert
October 20, 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;}