How to make at least one checkbox field mandatory from list of check box variables in variable set.

Raja Ravindra
Giga Contributor

Hi all, 

 

We created a variable set in a catalog item  with 6 Variables.

In that variable set 2 variables  are label type fields.

And 4 variables are check box types.

Requirement is to select at least one check box from the list of 4 checkbox variables before request submission. if no checkbox variable is marked as true, then we need to restrict the user for submitting the form by giving an alert.

Please help me with the script which we need to use for achieving this.

 

 

 

Thank you,

Raja.

 

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Raja Ravindra 

you need to use onSubmit catalog client script for this

Applies to Catalog Item - true

Applies on Catalog Item view - true

sample below

function onSubmit(){

var allowed = 'false';

// give here the 4 variable names in array

var checkboxArray = ['variableName1','variableName2','variableName3','variableName4'];

for(var i=0;i<checkboxArray.length;i++){

if(g_form.getValue(checkboxArray[i]).toString() == 'true'){

allowed = 'true';

break;

}

}

if(allowed == 'false'){

alert('Atleast 1 checkbox is to be selected');

return false;

}

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Tanushree Doiph
Mega Guru

Refer below code,

Add field names as per your requirement,

  if(g_form.getValue(field_name1) == 'true' ) || g_form.getValue(field_name2) == 'true' ) || g_form.getValue(field_name3) == 'true' ) || g_form.getValue(field_name4) == 'true' ))

{

return true

}

else

{

    alert('Please be sure to check the box before submitting a form.');

    return false;

}

Ankur Bawiskar
Tera Patron
Tera Patron

@Raja Ravindra 

you need to use onSubmit catalog client script for this

Applies to Catalog Item - true

Applies on Catalog Item view - true

sample below

function onSubmit(){

var allowed = 'false';

// give here the 4 variable names in array

var checkboxArray = ['variableName1','variableName2','variableName3','variableName4'];

for(var i=0;i<checkboxArray.length;i++){

if(g_form.getValue(checkboxArray[i]).toString() == 'true'){

allowed = 'true';

break;

}

}

if(allowed == 'false'){

alert('Atleast 1 checkbox is to be selected');

return false;

}

}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader