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

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;


  }


  }


I had just gotten it when you posted that! And now it seems to be working perfectly!! this is so awesome!!



I totally appreciate all your patience and going back and forth with me on this!!


You're welcome!