- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 08:08 AM - edited 10-18-2024 08:09 AM
We want to prevent the user from clicking more than a specific number of checkboxes. Is there code for this in a Catalog Client Script or UI Policy? What is the best practice?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 08:13 AM - edited 10-18-2024 08:14 AM
Hi @dmullinax,
Kudos to @Hitoshi Ozawa who has provided a solution to this exact question previously.
Check the below link:
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
For future usage and in case of broken links, here's the script courtesy of: @Hitoshi Ozawa
function onSubmit() {
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = 'checkbox1,checkbox2,checkbox3,checkbox4,checkbox5,checkbox6,checkbox7,checkbox8,checkbox9,checkbox10,checkbox11,checkbox12,checkbox13,checkbox14,checkbox15,checkbox16';
var mandatoryCount = 3;
var maxCount = 3;
var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if (!passed) {
//Abort the submit
alert('Please select ' + '3.');
return false;
}
function forceMandatoryCheckboxes(mandatory, count) {
mandatory = mandatory.split(',');
var cnt = 0;
for (var i = 0; i < mandatory.length; i++) {
if (g_form.getValue(mandatory[i]) == 'true') {
cnt++;
if (cnt > count) {
return false;
}
}
}
return (cnt == count);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 08:13 AM - edited 10-18-2024 08:14 AM
Hi @dmullinax,
Kudos to @Hitoshi Ozawa who has provided a solution to this exact question previously.
Check the below link:
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
For future usage and in case of broken links, here's the script courtesy of: @Hitoshi Ozawa
function onSubmit() {
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = 'checkbox1,checkbox2,checkbox3,checkbox4,checkbox5,checkbox6,checkbox7,checkbox8,checkbox9,checkbox10,checkbox11,checkbox12,checkbox13,checkbox14,checkbox15,checkbox16';
var mandatoryCount = 3;
var maxCount = 3;
var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if (!passed) {
//Abort the submit
alert('Please select ' + '3.');
return false;
}
function forceMandatoryCheckboxes(mandatory, count) {
mandatory = mandatory.split(',');
var cnt = 0;
for (var i = 0; i < mandatory.length; i++) {
if (g_form.getValue(mandatory[i]) == 'true') {
cnt++;
if (cnt > count) {
return false;
}
}
}
return (cnt == count);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 11:54 AM
Thank you so much. Is there any way to make it even more interactive? That is, in an onchange event?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 12:01 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2024 12:57 PM
I got it working with an onchange event. It would be great if there was a way I could centralize this code. Because for now I have to copy and paste this script into each onchange event that corresponds to the appropriate checkbox, but for now you got me going. Thanks!