UI Script to require at least one in a list of checkboxes be checked for form to be submitted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2018 02:07 PM
I am working on a donations form and I have a list of check boxes, at least one needs to be checked for the form to be submitted. I'd like a notice to popup on submit if the user did not select at least one checkbox.
I'm thinking a UI script on submit would do the trick but I fall short on how to write the section to check if at least one of the boxes is checked.
I found this in the question: onSubmit client script to prevent form submission
function onSubmit() {
if(g_form.getValue('ad_domain') == 'YES' || g_form.getValue('ad_domain') == 'YESb' && g_form.getValue('question_choice') == 'incorrect_info'){}
if(g_form.getValue(user_confirm') == 'false' ){ //this is the checkbox
alert('Please be sure to check the box before submitting a ticket.');
return false;
}
}
Kinda does the trick but it is looking at specific check boxes to be checked.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2018 09:18 AM
Yes.. It should be only one for the selection. Are you configuring only the first checkbox? It is a little tricky but you need to configure only your "Donation" checkbox.
Regards,
Pedro Lopez
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2018 05:33 AM
The following script, once modified with you values will do the trick.
function onSubmit(){
//Set the mandatory checkbox variable names and total mandatory count here
var mandatoryVars = 'agreement,agree_terms'; //checkbox values to check
var mandatoryCount = 2; //mandatory number of checkboxes
var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
if (!passed){
//Abort the submit
alert('You must agree to both of the ' + mandatoryCount + ' disclaimer terms.');
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;
}
Change the two VARS: var mandatoryVars and var mandatoryCount so they reflect your requirements as to the number of checkboxes and list the checkbox values (separated by a ,).
Also you can either modify the alert line or take it out; your choice. The script was something I used for a legal disclaimer checkbox confirmation on a form.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-09-2024 03:47 PM
This is exactly what I needed to solve an issue I was having, worked perfectly. Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2018 09:55 AM
You can try a code like this
function onSubmit() {
//Type appropriate comment here, and begin script below
if ((g_form.getValue('request_to') == "Add Access" || (g_form.getValue('request_to') == "Change Access"))){
if (g_form.getValue('dev_access') == "false" && g_form.getValue('qa_access') == "false" && g_form.getValue('access_cmsi') == "false"){
alert("'You cannot continue with the request without selecting at least one option (QA,Dev or CMSIPROD).");
return false;
}
}
}
dev_access and qa_access is the checkbox, instead of doing && you can probably do ||