How to make the Checkbox Mandatory

shaik_irfan
Tera Guru

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 

1 ACCEPTED SOLUTION

Ian Mildon
Tera Guru

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;
}

View solution in original post

13 REPLIES 13

dvp
Mega Sage
Mega Sage

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;
}

Sandeep Kumar6
Giga Guru

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

vbk1
Kilo Contributor

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

Ian Mildon
Tera Guru

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;
}

That exactly what i am looking for..