In this tutorial, you will learn how to make checkbox required in HTML. HTML checkboxes are used to let the users select one or more options on a given checkbox group. The checkboxes are shown square in shape when enabled. Sometimes when you are creating a form, you may want to create checkboxes that are must be selected (checked) by the users before submitting the form.
To make the checkbox required, we have to use the HTML
required attribute. The
required attribute is a Boolean attribute, which is used to specify that the user must select the required option before submitting the form. This required attribute can also be used in other form inputs like radio or text.
In the following example, we have 3 checkboxes and we will make one of them required. Please have a look over the code example and the steps given below.
- In the HTML file, we have multiple elements such as
input. The parent
div element is just a wrapper for the rest of the elements.
h2 heading tags have some text content.
- We have 3 inputs with type value set to
checkbox to select the course. The first checkbox has a
required attribute, which specifies that the user must select this option before submitting the form.
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<div><input type="checkbox" name="course" required>HTML</div>
<div><input type="checkbox" name="course">CSS</div>