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

One question Ian, can you please let me know the purpose of using ?

 if(g_form.getControl(mandatory[x])){

It's checking that you have an array value for 'mandatory'

vbk1
Kilo Contributor

Hi

I got a answer. By using UI policies give condition checkbox is false,checkbox1 is false in the sameway you give condition.After save you provide an action for any checkbox is mandatory.

Autometically form couldnot save without selecting an checkbox. i will copy my screenshot

find_real_file.png

Rahul Kumar17
Tera Guru

hi 

use this code

function onSubmit() {
//Type appropriate comment here, and begin script below
if(g_form.getValue('u_test1') == "true" || g_form.getValue('u_test2') == "true" ||g_form.getValue('u_test3') == "true"||g_form.getValue('u_test4') == "true" || g_form.getValue('u_test6') == "true" ||g_form.getValue('u_test6') == "true" )
{
return true;
}
else
{
alert("Select the checkbox");
return false;
}

}

If my response helped please mark it correct and close the thread.

Thanks,
Rahul Kumar