Make a selection mandatory for atleast one checkbox to be selected on a form with alert if nothing selected

Terry Carter Jr
Tera Contributor

Hello everyone!

I need some assistance today.  I have a custom application (IBM Resilient Integration) that I built out for my security response team and they would like to have a modification done to the checkboxes.  Yes, I have already approached them to use select boxes for the selections since it would be easier but they want the users to have to opportunity to select one or more checkbox while making a selection mandatory with an alert if nothing is selected.  

Below are the checkboxes that I'm using:

find_real_file.png

Any guidance or help is greatly appreciated!

Thanks,

Terry 

1 ACCEPTED SOLUTION

Yes, understood the problem.

The earlier code returns error when atleast one of them is false.

so instead of || use && and then it will fire only if all of those are unchecked.

View solution in original post

31 REPLIES 31

Okay I tried the code and I got no more errors.  Had to add a } as it was missing.

However, I received no alert and I was still able to submit the form with no checkbox selected. 

function onSubmit() {
//Type appropriate comment here, and begin script below
if(g_form.getValue("customer") == "" || g_form.getValue("non_customer") == "" || g_form.getValue("team_member") == "" || g_form.getValue("vendor") == "" || g_form.getValue("other") == "" ) {


g_form.addErrorMessage("Please select atleast 1 checkbox");


}
}

Okay.

So lets first check the values of this to understand it its empty or false. What it returns when not checked.

try this code and share the output screenshot of these messages.

function onSubmit() {
g_form.addInfoMessage("Customer value is"+g_form.getValue("customer"));
g_form.addInfoMessage("Non Customer value is"+g_form.getValue("non_customer"));
g_form.addInfoMessage("Vendor value is"+g_form.getValue("vendor"));
g_form.addInfoMessage("Team member value is"+g_form.getValue("team_member"));
g_form.addInfoMessage("Other value is"+g_form.getValue("other"));

//Type appropriate comment here, and begin script below
if(g_form.getValue("customer") == "" || g_form.getValue("non_customer") == "" || g_form.getValue("team_member") == "" || g_form.getValue("vendor") == "" || g_form.getValue("other") == "" ) {


g_form.addErrorMessage("Please select atleast 1 checkbox");


}
}

 

Okay below is the screenshot of the messages as requested.  I added unknown since that was a checkbox that was missing in the code. It still allowed me to also submit the form. 

find_real_file.png

okay, great.

So instead of empty, we shall try false

function onSubmit() {
//Type appropriate comment here, and begin script below
if(g_form.getValue("customer") == false || g_form.getValue("non_customer") == false || g_form.getValue("team_member") == false || g_form.getValue("vendor") == false || g_form.getValue("other") == false ) {


g_form.addErrorMessage("Please select atleast 1 checkbox");


}
}

Mark the comment as a correct answer and my other comment(s) as helpful once worked.

Alright we got alot further then we did before with using this code.  

See the error message listed below:

I really appreciate you helping me with this solution!

find_real_file.png