- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2019 02:52 AM
Hello,
I have 10 checkbox variables created we want users to select at least 1 checkbox otherwise it should throw an error.
How to acheive this
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2019 04:33 AM
I've used the below script in an onSubmit Client Script
function onSubmit(){
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = '<checkbox values here seperated by a ,>';
var mandatoryCount = 1;
var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if(!passed){
//Abort the submit
alert('You must select at least ' + mandatoryCount + ' options.');
return false;
}
}
function forceMandatoryCheckboxes(mandatory, count){
//Split the mandatory variable names into an array
mandatory = mandatory.split(',');
var answer = false;
var varFound = false;
var numTrue = 0;
//Check each variable in the array
for(x=0;x<mandatory.length;x++){
//Check to see if variable exists
if(g_form.getControl(mandatory[x])){
varFound = true;
//Check to see if variable is set to 'true'
if(g_form.getValue(mandatory[x]) == 'true'){
numTrue ++;
//Exit the loop if we have reached required number of 'true'
if(numTrue >= count){
answer = true;
break;
}
}
}
}
//If we didn't find any of the variables allow the submit
if(varFound == false){
answer = true;
}
//Return true or false
return answer;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2019 03:25 AM
As @kanchan suggested you need to write an onSubmit client script
and use the following script
if(g_form.getValue('u_checkbox1') != "true" && g_form.getValue('u_checkbox2') != "true" )
{
alert("Select the checkbox");
return false;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2019 03:30 AM
HI Shaik,
I would suggest you to create On-Submit catalog client script and create an array of all the checkbox variable.
then run a for or foreach loop to check whether they have true or false for every variable.
If non of them is having true then return false otherwise true
Sample :
var varss = ["var1", "var1", "var1"];
for(var i=0;i<varss.length;i++)
{
if(g_form.getvalue(varss[i])== true)
{
return true;
}
}
return false;
You can modify according to your need.
Please hit correct if this gave your answer.
Regards
Sandeep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2019 04:25 AM
HI
Please inform to user to Select atleast one checkbox, By using Label variable you can write a comment "Please select atleast one Checkbox in following".
Then user can understand it.
Or
Just mention a mandatory for Options.
Regards
Raja
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2019 04:33 AM
I've used the below script in an onSubmit Client Script
function onSubmit(){
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = '<checkbox values here seperated by a ,>';
var mandatoryCount = 1;
var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if(!passed){
//Abort the submit
alert('You must select at least ' + mandatoryCount + ' options.');
return false;
}
}
function forceMandatoryCheckboxes(mandatory, count){
//Split the mandatory variable names into an array
mandatory = mandatory.split(',');
var answer = false;
var varFound = false;
var numTrue = 0;
//Check each variable in the array
for(x=0;x<mandatory.length;x++){
//Check to see if variable exists
if(g_form.getControl(mandatory[x])){
varFound = true;
//Check to see if variable is set to 'true'
if(g_form.getValue(mandatory[x]) == 'true'){
numTrue ++;
//Exit the loop if we have reached required number of 'true'
if(numTrue >= count){
answer = true;
break;
}
}
}
}
//If we didn't find any of the variables allow the submit
if(varFound == false){
answer = true;
}
//Return true or false
return answer;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-26-2019 05:01 AM
That exactly what i am looking for..