- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 05:38 AM
I'm using a client script as follows and can't seem to get it working. When a checkbox is selected, I want the OnSubmit script to require one of three fields. When the checkbox is not selected, it throws a browser error onSubmit. If checkbox is selected, doesn't seem to function either. Here's what I have:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 05:55 AM - edited 01-09-2025 05:56 AM
I was able to get this to work using a much more simplified client script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 06:05 AM
Hi @MonicaW ,
Here is revised code.
function onSubmit() {
// Check if the checkbox is selected
if (g_form.getValue('personal_information') == 'true') {
var mandatoryVars = 'field1,field2,field3'; // Specify the fields to be checked
var mandatoryCount = 1; // Set the count for required fields
} else {
var mandatoryVars = ''; // No mandatory fields if checkbox isn't selected
var mandatoryCount = 0;
}
// Call the function to check the mandatory fields
var passed = forceMandatoryFields(mandatoryVars, mandatoryCount);
if (!passed) {
// Abort the submit and show an error message
alert('You must select at least ' + mandatoryCount + ' client information field(s).');
return false;
}
}
function forceMandatoryFields(mandatory, count) {
// If no mandatory fields, return true (nothing to check)
if (count === 0) {
return true;
}
// 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 (var x = 0; x < mandatory.length; x++) {
// Check if the variable exists
if (g_form.getControl(mandatory[x])) {
varFound = true;
// Check if the variable is not filled out (i.e., it's empty)
if (!g_form.getValue(mandatory[x])) {
numTrue++;
// Exit the loop if we've reached the required number of fields
if (numTrue >= count) {
answer = true;
break;
}
}
}
}
// If no variables are found, allow the submit
if (!varFound) {
answer = true;
}
// Return whether the mandatory fields are passed or not
return answer;
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 06:07 AM
try this simplified version
function onSubmit() {
var value1 = g_form.getValue('variable_1');
var value2 = g_form.getValue('variable_2');
var value3 = g_form.getValue('variable_3');
var value4 = g_form.getValue('variable_4');
if (g_form.getValue('personal_information') == 'true') {
if (!value1 && !value2 && !value3 && !value3) {
g_form.addErrorMessage("You must select at least 1 client information field.");
return false;
}
}
return true;
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 06:11 AM
Hi @MonicaW ,
I believe I also provided you the correct code based on your existing code..
As per new community feature you can mark multiple responses as correct.
If my response helped please mark it correct as well so that it benefits future readers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 05:55 AM - edited 01-09-2025 05:56 AM
I was able to get this to work using a much more simplified client script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 06:05 AM
Hi @MonicaW ,
Here is revised code.
function onSubmit() {
// Check if the checkbox is selected
if (g_form.getValue('personal_information') == 'true') {
var mandatoryVars = 'field1,field2,field3'; // Specify the fields to be checked
var mandatoryCount = 1; // Set the count for required fields
} else {
var mandatoryVars = ''; // No mandatory fields if checkbox isn't selected
var mandatoryCount = 0;
}
// Call the function to check the mandatory fields
var passed = forceMandatoryFields(mandatoryVars, mandatoryCount);
if (!passed) {
// Abort the submit and show an error message
alert('You must select at least ' + mandatoryCount + ' client information field(s).');
return false;
}
}
function forceMandatoryFields(mandatory, count) {
// If no mandatory fields, return true (nothing to check)
if (count === 0) {
return true;
}
// 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 (var x = 0; x < mandatory.length; x++) {
// Check if the variable exists
if (g_form.getControl(mandatory[x])) {
varFound = true;
// Check if the variable is not filled out (i.e., it's empty)
if (!g_form.getValue(mandatory[x])) {
numTrue++;
// Exit the loop if we've reached the required number of fields
if (numTrue >= count) {
answer = true;
break;
}
}
}
}
// If no variables are found, allow the submit
if (!varFound) {
answer = true;
}
// Return whether the mandatory fields are passed or not
return answer;
}
-------------------------------------------------------------------------
If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.
Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay
-------------------------------------------------------------------------
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 06:11 AM
Hi @MonicaW ,
I believe I also provided you the correct code based on your existing code..
As per new community feature you can mark multiple responses as correct.
If my response helped please mark it correct as well so that it benefits future readers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-09-2025 06:07 AM
try this simplified version
function onSubmit() {
var value1 = g_form.getValue('variable_1');
var value2 = g_form.getValue('variable_2');
var value3 = g_form.getValue('variable_3');
var value4 = g_form.getValue('variable_4');
if (g_form.getValue('personal_information') == 'true') {
if (!value1 && !value2 && !value3 && !value3) {
g_form.addErrorMessage("You must select at least 1 client information field.");
return false;
}
}
return true;
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader