Catalog item variable checkbox there are two option but only one should be selected

Bobby8
Tera Contributor

I tried to use the UI action to select a mandatory filed and applying the label to be set as a mandatory filed but i cannot able to achieve my condition form is getting submitted if both checkbox are selected.

 

Tried creating a below catalog script its also not working 

 

function onSubmit() {
var checkboxes = ['box1', 'box2'];
var selectedCount = 0;

for (var i = 0; i < checkboxes.length; i++) {
if (g_form.getValue(checkboxes[i]) == 'true') {
selectedCount++;
}
}

if (selectedCount !== 1) {
g_form.addErrorMessage('Please select only one checkbox.');
return false;
}
}

4 REPLIES 4

Harshad Wagh
Tera Guru

why cant you use multiple choice field? which will give option as radio button and allow only to select one option?

 

Thanks

Harshad

Anil Lande
Kilo Patron

Hi,

Are you getting the Error message you have added in your script?

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi,

Please try below:

function onSubmit() {
var checkboxes = ['box1', 'box2'];
var selectedCount = 0;

for (var i = 0; i < checkboxes.length; i++) {
if (g_form.getValue(checkboxes[i]) == 'true') {
selectedCount++;
}
}

if (selectedCount != 1) {
g_form.addErrorMessage('Please select only one checkbox.');
return false;
}
}
Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Samaksh Wani
Giga Sage
Giga Sage

Hello @Bobby8 

function onLoad(){

var chk1 = g_form.getControl('chkfield1_name');
var chk2 = g_form.getControl('chkfield2_name');

var count = 0;

If(chk1.checked){
count++;
}
If(chk2.checked){
count++;
}

if(count==1){
return true;
}
else{
return false;
}

}

Plz use this script :-

 

Mark my solution as Accept if you find it helpful.

 

Regards

SAMAKSH