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

Pooja Mallikarj
Kilo Sage

Hi,

If you have checkbox variable as true/false ,then you can not make mandatory. Since a checkbox only contains a true or false value, it always contains SOME value, which makes mandatory kind of useless.

If you have other options in checkbox then,you can make checkbox mandatory based on condition.

By using onChange, onSubmit and  onLoad client script.

 

You can do an onSubmit client script that does something like this:

var checked = g_form.getValue('MyCheckbox');

if (!checked) {

        g_form.addErrorMessage('You didn't check the checkbox!');

        return false;

}

 

Mark correct/helpful if it helps for you.

Regards,

Pooja

This can be done for a single checkbox but i have 10 checkbix in which atleast if 1 checkbox is checked then it should pass only if the all the 10 checkbox are not filled it should fail. 

 

Alex Ward
Kilo Guru

Perhaps use a scripted Catalogue UI Policy?

If x & y & z variables are all false, make x & y & z variables mandatory. When one of the variables becomes True, they are no longer mandatory

Inactive_Us1957
Kilo Guru

You can write the onSubmit Client script and code will be like
if(checkbox1 != "true" && checkbox2 != "true" && checkbox2 != "true")//add the all checkboxes name including &&
{
alert("Select the checkbox");
return false;
}