16

I have a form there are 4 options (they may be checkbox or radio).

I want to select multiple options but one is compulsory.

I know it is possible in JS/jQuery but I want a HTML/CSS based solution.

om_jaipur
  • 2,176
  • 4
  • 30
  • 54
  • 8
    Why tag the question with javascript if you want a solution without javascript? – Arg0n Nov 21 '15 at 11:25
  • 3
    HTML5 has this feature, Refer this: [HTML5: How to use the “required” attribute with a “radio” input field](http://stackoverflow.com/questions/8287779/html5-how-to-use-the-required-attribute-with-a-radio-input-field) – Sohair Ahmad Nov 24 '15 at 07:57

5 Answers5

41

To be able to check multiple inputs, they must be checkboxes. (They could be radio buttons with different names, but you wouldn't be able to uncheck them once checked.)

So use checkboxes, and show the Submit button only if any are checked, using the general sibling selector (~):

input[type="Submit"] {
  display: none;
}

input:checked ~ input[type="Submit"] {
  display: inline;
}
<input id="c1" type="checkbox"><label for="c1">First</label><br>
<input id="c2" type="checkbox"><label for="c2">Second</label><br>
<input id="c3" type="checkbox"><label for="c3">Third</label><br>
<input id="c4" type="checkbox"><label for="c4">Fourth</label><br>
<input type="Submit">

If you want the appearance of a disabled submit button, add a second button that is disabled.

When no input is clicked, show the disabled submit button only. When one or more inputs are clicked, show the enabled submit button only:

input[type="Submit"]:not([disabled]) {
  display: none;
}

input:checked ~ input[type="Submit"]:not([disabled]) {
  display: inline;
}

input:checked ~ input[disabled] {
  display: none;
}
<input id="c1" type="checkbox"><label for="c1">First</label><br>
<input id="c2" type="checkbox"><label for="c2">Second</label><br>
<input id="c3" type="checkbox"><label for="c3">Third</label><br>
<input id="c4" type="checkbox"><label for="c4">Fourth</label><br>

<input type="Submit" disabled>
<input type="Submit">
Rick Hitchcock
  • 35,202
  • 5
  • 48
  • 79
13

Further to the answer of @Rick Hitchcock, I think that you will want to show to the user the button submit but it will disabled until one of the checkboxes will be checked.

If so, you can use pointer-events (in all modern browsers: http://caniuse.com/#feat=pointer-events) like this:

input[type="Submit"] {
  opacity:0.5;
  pointer-events:none;
  /* animation added for fancy ;) */
  transition:all .2s ease;
}

input:checked ~ .button-wrapper input[type="Submit"] {
  opacity:1;
  pointer-events:all;
}

.button-wrapper {
  position:relative;  
  display:inline-block;
}

.button-wrapper:before {
  content:"";
  position:absolute;
  top:0;
  left:0;
  width:100%;
  height:100%;
  z-index:1;
}

input:checked ~ .button-wrapper:before {
  display:none;  
}
<input id="c1" type="checkbox"><label for="c1">First</label><br>
<input id="c2" type="checkbox"><label for="c2">Second</label><br>
<input id="c3" type="checkbox"><label for="c3">Third</label><br>
<input id="c4" type="checkbox"><label for="c4">Fourth</label><br>
<div class="button-wrapper">
  <input type="Submit" tabindex="-1">
</div>

Edit I was added a "mask" in .button-wrapper:before so it will work in the old browsers.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • `pointer-events` does not work in IE10 and below. If a user is browsing in IE10, they will be able to click the button without checking any checkboxes. – razemauze Nov 24 '15 at 13:28
  • 1
    @rblarsen, thank you for your comment. I was updated my answer to support the old too. I was mention that `pointer-events` works on `modern browsers` and added the `caniuse` link. Also, he need to validate the submit event anyway (server at least) it's not that problem to send the form even the button are hidden ;) – Mosh Feu Nov 24 '15 at 13:42
  • You can get around `pointer-events` not working in `IE10 and below` with the following JS workaround: http://codepen.io/jonathan/pen/BriCb .. Also IE11 allows pointer-events but only if links `display` property is set to `block` or `inline-block`. – Jonathan Marzullo Nov 24 '15 at 14:40
  • It is still possible to submit an invalid form using tab to navigate and pressing space. – falsarella Nov 30 '15 at 22:12
  • 1
    @falsarella, You can submit the form within the browser in any way. This validation it's just for the UX for users who are not trying to hack. In any way, you have to validate the input in the server. – Mosh Feu Nov 30 '15 at 23:08
  • Despite I kind of agree, that's still trying to answer what OP asked. Anyway, [we found a better way](http://stackoverflow.com/a/33842776/1064325). – falsarella Nov 30 '15 at 23:17
  • @falsarella I'm happy that you found a better way ;) You have a point, so I was updated my answer. I was added `tabindex=-1` to the submit button, so you can't access it using `tab`. – Mosh Feu Dec 01 '15 at 05:07
10

You can do this in html5 using the required attribute Like

<input type="checkbox" required name="your_checkbox_name">

This tells the browser that the form should not be to submitted without the checkbox being checked.Although i recommend java-script since not all browsers will be able to recognize this.

Or

If you want to detect if at least one check box is selected as suggested by @RickHitchcock in the comments,You could use

span {
  display: inline;
  color: red;
}
input[type="Submit"],
input:checked ~ span {
  display: none;
}
input:checked ~ input[type="Submit"] {
  display: inline;
}
<form action="#" method="post">
  <input type="checkbox" />Checkbox 1
  <br />
  <input type="checkbox" />Checkbox 1
  <br />
  <input type="checkbox" />Checkbox 1
  <br />
  <input type="submit" value="Submit" /><span>! Please check at least one checkbox</span>

</form>
Collins Abitekaniza
  • 4,496
  • 2
  • 28
  • 43
6

You can use the following for which one is compulsory.

<input type="radio" name="name" required>

Which one without required will not be tested if it is ticked or not.

Jahanzaib Asgher
  • 252
  • 1
  • 11
6

Try This:

<input id="c3" type="checkbox" required><label for="c3">Third</label><br>
<input id="c4" type="checkbox" required><label for="c4">Fourth</label><br>

Or you can try this using jquery to validate a html checkbox:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Makes "field" always required. Nothing and blanks are invalid.  </title>
<link rel="stylesheet" href="http://jqueryvalidation.org/files/demo/site-demos.css">

</head>
<body>
<form id="myform">
<label for="field">Required: </label>
<input type="text" class="left" id="field" name="field">
<br/>
<input type="submit" value="Validate!">
</form>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js">      </script>
 <script src="http://jqueryvalidation.org/files/dist/additional- methods.min.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
$( "#myform" ).validate({
  rules: {
field: {
  required: true
}
  }
});
</script>
</body>
</html>

required is the way html validates things

The Beast
  • 1
  • 1
  • 2
  • 24
  • Have you read my question properly, "I want to select multiple options but **one is compulsory**." not all of these are compulsory... – om_jaipur Dec 01 '15 at 04:55