×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Checkboxes - At Least 1 is Selected?

Checkboxes - At Least 1 is Selected?

Checkboxes - At Least 1 is Selected?

(OP)
Hi guys,

I have this checkboxes:

<input name="BookBox" id="BookBox" type="checkbox" value="12" />Assess
<input name="BookBox" id="BookBox" type="checkbox" value="14" />Communication
<input name="BookBox" id="BookBox" type="checkbox" value="4" />Family
<input name="BookBox" id="BookBox" type="checkbox" value="3" />Document
<input name="BookBox" id="BookBox" type="checkbox" value="6" />Computer

How do you make the visitor to click/check at least one box before submitting the form?

Thanks for any ideas.

RE: Checkboxes - At Least 1 is Selected?

Hi

For example by calling onsubmit a function like this :

CODE --> JavaScript

function checkCheckbox(event)
{
  if (! Array.from(document.querySelectorAll('input[name="BookBox"]')).filter(i => i.checked).length) {
    event.preventDefault()
    alert('At least one checkbox must be checked')
  }
} 
  • See it in action at https://jsfiddle.net/cdmy75ap/
  • Used just ECMAScript 6, no library.
  • To keep it simple, this checks all inputs with name "BookBox", regardless whether they are checkboxes or not or belong to the form about to submit or not.
    Just change the .querySelectorAll() call to event.target.querySelectorAll('input[type="checkbox"][name="BookBox"]') to restrict it to checkboxes in the submitted form.
  • Note that id must be unique per document. Using the same id for multiple elements like in your original code, may cause weird behavior later.

Feherke.
feherke.github.io

RE: Checkboxes - At Least 1 is Selected?

(OP)
Thanks feherke ..Thats awesome! Have a good day.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close