- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2016 11:32 AM
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!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2016 11:43 AM
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;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2016 01:37 PM
This should work:
function onSubmit(){
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = 'first,second,third,fourth,fifth,sixth';
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])){
alert('found control');
if(g_form.getValue(mandatory[x]) == 'true'){
alert('adding to true max');
numTrueMax ++;
x++;
if(mandatory[x] != 'sixth'){
alert('adding to true man');
numTrueMan ++;
}
}
else{
x++;
}
}
}
if(numTrueMax < 5 && numTrueMan > 0){
answer = true;
}
else{
answer = false;
}
return answer;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2016 01:40 PM
But you'll want to remove the alerts, was using them to debug.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2016 05:58 AM
This is sooo close! but it is still letting me submit a request if u_email_choice is the only one selected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2016 06:17 AM
Ah, it was a misplacement of the x++ in the if loop:
if(g_form.getControl(mandatory[x])){
if(g_form.getValue(mandatory[x]) == 'true'){
numTrueMax ++;
if(mandatory[x] != 'sixth'){
numTrueMan ++;
}
x++;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2016 11:35 AM
I know I have probably butchered this script, while loops are not my forte. at all. This seems to only let me submit a request if u_Boolean_6 is true. so it's, not going through the loop correctly.
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++;
}
if(numTrueMax < 5 && numTrueMan > 0){
answer = true;
}
else{
answer = false;
}
return answer;
}
}
}
}