Client script to make 1 of 3 fields mandatory if a checkbox is selected

MonicaW
Tera Guru

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:

function onSubmit() {
if (g_form.getValue('personal_information') == 'true')
var mandatoryVars = 'field1,field1,field3';
var mandatoryCount = 1;

var passed = forceMandatoryFields(mandatoryVars, mandatoryCount);
if (!passed) {
//Abort the submit
alert('You must select at least ' + mandatoryCount + ' client information field.');
return false;
}
}

function forceMandatoryFields(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]))) {
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;
}

 

4 ACCEPTED SOLUTIONS

MonicaW
Tera Guru

I was able to get this to work using a much more simplified client script:

function onSubmit() {
if(g_form.getValue('client_information') == 'true'){
if((g_form.getValue('field1')=='') && (g_form.getValue('field2')=='') && g_form.getValue('field3')==''){
alert('You must fill out at least one patient information field.');
return false;
}else{
return true;
}
}else{
return true;
}
}

View solution in original post

Runjay Patel
Giga Sage

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

-------------------------------------------------------------------------

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@MonicaW 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

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.

View solution in original post

5 REPLIES 5

MonicaW
Tera Guru

I was able to get this to work using a much more simplified client script:

function onSubmit() {
if(g_form.getValue('client_information') == 'true'){
if((g_form.getValue('field1')=='') && (g_form.getValue('field2')=='') && g_form.getValue('field3')==''){
alert('You must fill out at least one patient information field.');
return false;
}else{
return true;
}
}else{
return true;
}
}

Runjay Patel
Giga Sage

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

-------------------------------------------------------------------------

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.

Ankur Bawiskar
Tera Patron
Tera Patron

@MonicaW 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader