checkbox maximum

alexcharleswort
Tera Expert

I have a need to set a max limit on a set of checkbox variables. I have one already in place to set a minimum (the minimum should be one and the max should be 4). I can't quite seem to get it right, though.

This is the one set for the minimum:

function onSubmit(){


        //Set the mandatory checkbox variable names and total mandatory count here
        var mandatoryVars = 'u_boolean_6,u_toll_free,u_fax_choice,u_ext_choice,u_cell';
        var mandatoryCount = 1;
       
        var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
        if(!passed){
                //Abort the submit
                alert('You must select at least ' + mandatoryCount + ' option for contact information besides email.');
                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;
}

Any ideas on this?

All help would be much appreciated!

1 ACCEPTED SOLUTION

It's definitely a learning curve. Looks like you were missing the "else" loop that advances the count if it doesn't find a true value in your first option, and then you had embedded the final assessment of the counts within the while loop, but you want it to run after the while loop is done:



function onSubmit(){


  //Set the mandatory checkbox variable names and total mandatory count here


  var mandatoryVars = 'u_boolean_6,u_toll_free,u_fax_choice,u_ext_choice,u_cell,u_email_choice';


  var mandatoryCount = 1;


  var maxCount = 4;



  var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);


  if(!passed){


  //Abort the submit


  alert('You must select at least ' + mandatoryCount + ' option for contact information besides email.');


  return false;


  }


  function forceMandatoryCheckboxes(mandatory, count){


  //Split the mandatory variable names into an array


  mandatory = mandatory.split(',');


  var answer = false;


  var varFound = false;


  var numTrueMan = 0;


  var numTrueMax = 0;


  var x = 0;


  //Check each variable in the array


  while(mandatory[x]){


  //   alert('starting mandatory loop with mandatory being ' + mandatory[x]);


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


  if(g_form.getValue(mandatory[x]) == 'true'){


  numTrueMax ++;


  if(mandatory[x] != 'u_email_choice'){


  numTrueMan ++;


  }


  x++;


  }


  else{


  x++;


  }


  }


  }


  if(numTrueMax < 5 && numTrueMan > 0){


  answer = true;


  }


  else{


  answer = false;


  }


  return answer;


  }


  }


View solution in original post

12 REPLIES 12

kristenankeny
Tera Guru

Are you looking to handle the check with one script? If so, I would use a while loop to count the number of true variables, then use an if statement to see if it is greater than 0 and less than 5.


I would like to do them in one but I think that would end up being a little complicated and hard for the other people on my team to read. I actually have checkboxes A, B, C, D, E, and F. But F doesn't count towards the minimum. So the first script, which is done, will say they have to pick at least one between A, B, C, D, or E. And in the max script, F does count. so that one should say no more than 4 can be checked between A, B, C, D, E or F.


I think you could still handle that in a single script.



function onSubmit(){






        //Set the mandatory checkbox variable names and total mandatory count here


        var mandatoryVars = 'u_boolean_6,u_toll_free,u_fax_choice,u_ext_choice,u_cell,your_optional_var';


        var mandatoryCount = 1;


  var maxCount = 4;


       


        var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);


        if(!passed){


                //Abort the submit


                alert('You must select at least ' + mandatoryCount + ' option for contact information besides email.');


                return false;


        }




}


function forceMandatoryCheckboxes(mandatory, count){


        //Split the mandatory variable names into an array


        mandatory = mandatory.split(',');


        var answer = false;


        var varFound = false;


        var numTrueMan = 0;


  var numTrueMax = 0;


  //Check each variable in the array


        while(madatory.next()){


  if(g_form.getControl(mandatory)){


  if(g_form.getValue(mandatory == 'true'){


  numTrueMax ++;


  if(mandatory != 'your_optional_var'){


  numTrueMan ++;


  }


  }


  }


  }



  if(numTrueMax < 5 && numTrueMan > 0){


  answer = true;


  }


  else{


  answer = false;


  }


  return answer;


}


I keep getting this error....


min.PNG