Submitting form jQuery checkboxlist In my requestForm submit function I have this logic that checks three checkboxes and then displays the label with an error message. Is there better logic? I would be happy for any kind of feedback since this is quite critical a part of my application.
$(&quot;#requestForm&quot;).submit(function (e) {
            e.stopPropagation();
            e.preventDefault();


        var checked_sourcecheckboxes = $(&quot;#sources input[type=checkbox]:checked&quot;);
        if (checked_sourcecheckboxes.length == 0) {
            $(&quot;#lblSourcesError&quot;).show();
            return false;
        }
        else {
            $(&quot;#lblSourcesError&quot;).hide();
        }
        var checked_academicformatcheckboxes = $(&quot;#academicFormatsRow input[type=checkbox]:checked&quot;);
        if (checked_academicformatcheckboxes.length == 0) {
            $(&quot;#lblacademicFormatError&quot;).show();
            return false;
        }
        else {
            $(&quot;#lblacademicFormatError&quot;).hide();
        }
        var checked_academicmediaformatcheckboxes = $(&quot;#academicMediaFormatRow input[type=checkbox]:checked&quot;);
        if (checked_academicmediaformatcheckboxes.length == 0) {
            $(&quot;#lblacademicmediaFormatError&quot;).show();
            return false;
        }
        else {
            $(&quot;#lblacademicmediaFormatError&quot;).hide();
        }
})
Main Question

Is there better logic?

Repeated code
The first thing that I see with this code, which perhaps you see also, is that it is quite repetitive - select a list of checkboxes to see if any are checked - if none are checked then show an error message and return, otherwise hide any affiliated error message. This violates the Don't Repeat Yourself principle. It can be remedied by abstracting the common logic - e.g. putting the  the selectors into a mapping:
var SELECTOR_ERROR_MAPPING = {
    &quot;#sources input[type=checkbox]:checked&quot;: &quot;#lblSourcesError&quot;, 
    &quot;#academicFormatsRow input[type=checkbox]:checked&quot;: &quot;lblacademicFormatError&quot;,
    //...more entries
};
        

Then use a loop to iterate over the mapping, checking each selector - e.g. using Array.every():
return Object.keys(SELECTOR_ERROR_MAPPING).every(function(selector) {
    var errorSelector = SELECTOR_ERROR_MAPPING[selector];

    var showError = !$(selector).length;
    $(errorSelector).toggle(showError, errorSelector);
    return !showError;
});

Those last three lines of the inner function could be placed into a separate function if desired, which would allow for better modularization/atomization and testing.
demo


var SELECTOR_ERROR_MAPPING = {
  "#sources input[type=checkbox]:checked": "#lblSourcesError",
  "#academicFormatsRow input[type=checkbox]:checked": "#lblacademicFormatError",
  "#academicMediaFormatRow input[type=checkbox]:checked": "#lblacademicmediaFormatError",
  //...more entries
};
$("#requestForm").submit(function(e) {
  e.stopPropagation();
  e.preventDefault();

  var allValid = Object.keys(SELECTOR_ERROR_MAPPING).every(function(selector) {
    var errorSelector = SELECTOR_ERROR_MAPPING[selector];

    var showError = !$(selector).length;
    $(errorSelector).toggle(showError, errorSelector);
    return !showError;
  });
  console.log('result', allValid);
  return allValid;
})
.error {
  color: red;
}
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
&lt;form id="requestForm"&gt;
  &lt;div id="sources"&gt;
    &lt;h3&gt; Source &lt;/h3&gt;
    &lt;label&gt;Audi 
      &lt;input type="checkbox" name="source" value="Audi" /&gt;
    &lt;/label&gt;
    &lt;label&gt;BMW 
      &lt;input type="checkbox" name="source" value="BMW" /&gt;
    &lt;/label&gt;
    &lt;div class="error" id="lblSourcesError" style="display: none"&gt;Please select a source&lt;/div&gt;
  &lt;/div&gt;
  &lt;div id="academicFormatsRow"&gt;
    &lt;h3&gt; Formats &lt;/h3&gt;
    &lt;label&gt;College 
      &lt;input type="checkbox" name="source" value="college" /&gt;
    &lt;/label&gt;
    &lt;label&gt;University
      &lt;input type="checkbox" name="source" value="university" /&gt;
    &lt;/label&gt;
    &lt;div class="error" id="lblacademicFormatError" style="display: none"&gt;Please select a format&lt;/div&gt;

  &lt;/div&gt;
  &lt;div id="academicMediaFormatRow"&gt;
    &lt;h3&gt; Media Formats &lt;/h3&gt;
    &lt;label&gt;DVD 
      &lt;input type="checkbox" name="source" value="DVD" /&gt;
    &lt;/label&gt;
    &lt;label&gt;Blue-ray
      &lt;input type="checkbox" name="source" value="blueray" /&gt;
    &lt;/label&gt;
    &lt;div class="error" id="lblacademicmediaFormatError" style="display: none"&gt;Please select a media format&lt;/div&gt;

  &lt;/div&gt;
  &lt;button&gt;Submit&lt;/button&gt;
&lt;/form&gt;



Modern browsers support client-side validation using the required attribute on input elements1 and it is supported for checkbox inputs2 but apparently setting a custom message for checkboxes still involves writing some JavaScript3.
HTML layout
As one comment suggested, the dependence on the id attribute could be decreased - For example, if the location of the error message element was consistent within each section -e.g. a sibling of the list of checkbox inputs. Another solution might be to have each section of checkboxes and error message contained by a parent element with a consistent class name- then the JavaScript could loop over those container elements looking to see if the checkbox elements have any checked- if not, then display the error message.
Other review points
ES6
With modern JavaScript, consider using newer EcmaScript-2015 (AKA ES6) features, like for...of loops, the keywords let and const instead of var, etc.
jQuery
Also consider whether you really need jQuery because it may be the case it can be replaced with “vanilla JS” features. For example, to select elements with a CSS selector use document.querySelectorAll() and to add an event listener for the form submission, use the addEventListener() method after selecting that form element - e.g. with document.getElementById('requestForm'), document.forms[0] unless it isn't the first form, etc..
Loose comparisons
In the original code the length property of the jQuery objects was compared with 0 using loose equality - i.e. ==. A good habit and recommendation of many style guides is to use strict equality operators (i.e. ===, !==) whenever possible. The problem with loose comparisons is that it has so many weird rules one would need to memorize in order to be confident in its proper usage.
The suggested code above and below simplified this by using !$(checkboxSelector).length;
demo


const SELECTOR_ERROR_MAPPING = {
  "#sources input[type=checkbox]:checked": "#lblSourcesError",
  "#academicFormatsRow input[type=checkbox]:checked": "#lblacademicFormatError",
  "#academicMediaFormatRow input[type=checkbox]:checked": "#lblacademicmediaFormatError",
  //...more entries
};
document.forms[0].addEventListener('submit', e =&gt; {
  e.stopPropagation();
  e.preventDefault();

  for (const [selector, errorSelector] of Object.entries(SELECTOR_ERROR_MAPPING)) {
    const showError = !document.querySelectorAll(selector).length;
    document.querySelector(errorSelector).classList.toggle('hidden', !showError);
    if (showError) {
      return false;
    }
  }
});
.error {
  color: red;
}
.hidden {
  display: none;
}
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
&lt;form id="requestForm"&gt;
  &lt;div id="sources"&gt;
    &lt;h3&gt; Source &lt;/h3&gt;
    &lt;label&gt;Audi 
      &lt;input type="checkbox" name="source" value="Audi" /&gt;
    &lt;/label&gt;
    &lt;label&gt;BMW 
      &lt;input type="checkbox" name="source" value="BMW" /&gt;
    &lt;/label&gt;
    &lt;div class="error hidden" id="lblSourcesError"&gt;Please select a source&lt;/div&gt;
  &lt;/div&gt;
  &lt;div id="academicFormatsRow"&gt;
    &lt;h3&gt; Formats &lt;/h3&gt;
    &lt;label&gt;College 
      &lt;input type="checkbox" name="source" value="college" /&gt;
    &lt;/label&gt;
    &lt;label&gt;University
      &lt;input type="checkbox" name="source" value="university" /&gt;
    &lt;/label&gt;
    &lt;div class="error hidden" id="lblacademicFormatError"&gt;Please select a format&lt;/div&gt;

  &lt;/div&gt;
  &lt;div id="academicMediaFormatRow"&gt;
    &lt;h3&gt; Media Formats &lt;/h3&gt;
    &lt;label&gt;DVD 
      &lt;input type="checkbox" name="source" value="DVD" /&gt;
    &lt;/label&gt;
    &lt;label&gt;Blue-ray
      &lt;input type="checkbox" name="source" value="blueray" /&gt;
    &lt;/label&gt;
    &lt;div class="error hidden" id="lblacademicmediaFormatError"&gt;Please select a media format&lt;/div&gt;

  &lt;/div&gt;
  &lt;button&gt;Submit&lt;/button&gt;
&lt;/form&gt;