Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8011013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:54:23+00:00 2026-06-04T18:54:23+00:00

I currenlty have some javascript/jquery code that displays an error message if no boxes

  • 0

I currenlty have some javascript/jquery code that displays an error message if no boxes are checked. However when I check a box and submit the form the error message still displays.

any help would be great!

javascript code:

    var hobbies = $('#hobbies').val();
    if ($('#hobbies :checkbox:checked').length === 0){
    $("#multichk").after('<span class="error">Please choose at least one.</span>');
    hasError = true;
    }

html code:

 <input type='checkbox' id='hobbies' name='hobbies[0]' value='1'/><label for='hobbies0'>football</label><br />

 <input type='checkbox' id='hobbies' name='hobbies[1]' value='2'/><label for='hobbies1'>soccer</label><br />

 <input type='checkbox' id='hobbies' name='hobbies[2]' value='3'/><label for='hobbies2'>baseball</label><br />

 <input type='checkbox' id='hobbies' name='hobbies[3]' value='4'/><label for='hobbies3'>tennis</label><br />

 <input type='hidden' id='multichk' />

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T18:54:25+00:00Added an answer on June 4, 2026 at 6:54 pm

    The way you have the checkboxes named and identified, they won’t be part of the same group, and they won’t be properly identified.

    Each element on a page, when identified, must have a unique ID, and for a group of checkboxes, or radio buttons for that matter, to be validated and delivered as a group, each member of the group must have the same name.

    Given these rules, it should come as no surprised that the for attribute of a label tag references an element’s id attribute, rather than its name attribute.

    Now, I’m guessing that you’re using PHP for your server-side language (based on the brackets in the checkbox names), in which case the checkboxes should be declared like so:

    <input type='checkbox' id='hobbies0' name='hobbies[]' value='1'/>
    <label for='hobbies0'>football</label><br />
    
    <input type='checkbox' id='hobbies1' name='hobbies[]' value='2'/>
    <label for='hobbies1'>soccer</label><br />
    
    <input type='checkbox' id='hobbies2' name='hobbies[]' value='3'/>
    <label for='hobbies2'>baseball</label><br />
    
    <input type='checkbox' id='hobbies3' name='hobbies[]' value='4'/>
    <label for='hobbies3'>tennis</label><br />
    

    [Note: labels only moved to next line to remove horizontal scroll in code box]

    With the HTML straightened out, the jQuery would look something like this:

    if($('input:checkbox[name="hobbies[]"]:checked').length === 0){
        $("#multichk").after('<span class="error">Please choose at least one.</span>');
        hasError = true;
    }
    

    Reference for selector format

    If that selector still gives you trouble, then you may need to prefix the square brackets with double backslashes like this:

    if($('input:checkbox[name="hobbies\\[\\]"]:checked').length === 0){
    

    Reference for double backslashes

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that currently checks for minimum and maximum lentgh. I want
I'm trying to write some jQuery code that will highlight the element the cursor
I have some code that fades in the container div of the home page
I have some PHP AJAX code that is supposed to validate some parameters sent
I currently have some code which when you press the submit button, a marker
I'm using Zend_Search_Lucene, the PHP port of Java Lucene. I currently have some code
We have some stuff that may be exported into various formats. Currently we have
I currently have a table structure that looks something like this(some details omitted): ColumnName
Currently, I have some code as follows template<typename Type> Type* getValue(std::string name, bool tryUseGetter
I currently have a Rails website that has some Prototype scripting in it. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.