HTML - required Attribute



The HTML required attribute is a boolean attribute. It is used to specify that an input field must be filled by users before submitting the form if the required attribute is present in the input field.

This attribute works with the text, search, email, number, checkbox, radio, date and file input types.

This attribute will also works with <textarea> and <select>.

Syntax

Following is the syntax of the HTML required attribute −

<tag required></tag>

Example

In the following example, we are using the HTML ‘required’ attribute within the input type = text.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'required' attribute</title>
</head>
<body>
   <!--HTML 'required' attribute-->
   <p>Example of the HTML 'required' attribute</p>
   <form>
      <label>First Name: </label>
      <input type="text" required>
      <button>Submit</button>
   </form>
</body>
</html>

When we run the above code, it will generate an output consisting of the input field mentioned with required attribute along with a click button displayed on the webpage.

Example

Consider the another scenario, where we are going to use the required attribute with the select option.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'required' attribute</title>
</head>
<body>
   <!--HTML 'required' attribute-->
   <p>Example of the HTML 'required' attribute</p>
   <form>
      <p>Select your Language: </p>
      <select required>
      <option value="">--Choose your option--</option>
      <option value="">Hindi</option>
      <option value="">English</option>
      <option value="">Telgu</option>
      </select>
      <button>Submit</button>
   </form>
</body>
</html>

On running the above code, the output window will pop up displaying the dropdown menu along with a click button on the webpage.

Example

Let's look at the following example, where we are going to use the required attribute with the textarea tag.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'required' attribute</title>
</head>
<body>
   <!--HTML 'required' attribute-->
   <p>Example of the HTML 'required' attribute</p>
   <form>
      <p>Write your address: </p>
      <textarea cols="40" rows="8" placeholder="Write..."></textarea>
      <br>
      <button>Submit</button>
   </form>
</body>
</html>

When we run the above code, it will generate an output consisting of the textarea field along with a click button displayed on the webpage.

Example

Following is the example, that shows the usage of the ‘required’ attribute(property) with JavaScript.

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'required' attribute</title>
</head>
<body>
   <!--HTML 'required' attribute-->
   <p>Example of the HTML 'required' attribute</p>
   <form> Name: <input type="text" placeholder="Your name" id='demo'>
      <button>Submit</button>
      <p>Click on the below buttons to set/remove the required from the above input field.</p>
      <button onclick="Add()">Set required</button>
      <button onclick="Remove()">Remove required</button>
   </form>
   <script>
      function Add() {
         document.getElementById('demo').required = true;
         alert("required set successfully.")
      }

      function Remove() {
         document.getElementById('demo').required = false;
         alert("required remove successfully.")
      }
   </script>
</body>
</html>

On running the above code, the output window will pop up displaying the input field along with a click buttons on the webpage. when the user clicks the button the event gets triggered and displays an alert.

html_attributes_reference.htm
Advertisements