Open In App

HTML required Attribute

Last Updated : 29 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

It is a Boolean attribute that is used to specify that the input element must be filled out before submitting the Form.

Elements: These attributes can be associated with three elements which are listed below:  

Syntax:

<input required>

Example-1: In this example, we demonstrate the use of the required attribute in a form. The attribute ensures that the “Username” field must be filled out before submitting the form.

html
<!DOCTYPE html>
<html>

<head>
    <title>required Attribute</title>
    <style>
        h1, h2 {
            color:green;
            font-style:italic;
        }
        body {
            text-align:center;
        }
    </style>
</head>

<body>
    <h1>GeeksForGeeks</h1>
    <h2>required attribute in input Field</h2>
    <form action="">
        Username:
        <input type="text" 
               name="username" 
               required>
        <br>
        Password:
        <input type="password" 
               name="password">
        <br>
        <input type="submit">
    </form>
</body>

</html>

Output: 

Syntax:

<select required>

Example-2: In this example we demonstrates the required attribute in a <select> field. It ensures users select an option other than “None” before submitting the form, styled with green italic headings.

html
<!DOCTYPE html>
<html>

<head>
    <title>required Attribute</title>
    <style>
        h1, h2 {
            color:green;
            font-style:italic;
        }
        body {
            text-align:center;
        }
    </style>
</head>

<body>
    <h1>GeeksForGeeks</h1>
    <h2>required attribute in select Field</h2>
    
    <form action="">
        <select required>
            <option value="">
                None
            </option>
            <option value="ds">
                Data Structure
            </option>
            <option value="algo">
                Algorithm
            </option>
            <option value="os">
                Operating System
            </option>
            <option value="cn">
                Computer Network
            </option>
        </select>
        <input type="submit">
    </form>
    
</body>

</html>

Output: 

Syntax:

<textarea required>

Example-3: In this example we demonstrates the required attribute in a <textarea> field, ensuring users provide input before form submission. The page includes green, italic headings and a centered layout.

html
<!DOCTYPE html>
<html>

<head>
    <title>required Attribute</title>
    <style>
        h1, h2 {
            color:green;
            font-style:italic;
        }
        body {
            text-align:center;
        }
    </style>
</head>

<body>
    <h1>GeeksForGeeks</h1>
    <h2>required attribute in Textarea Field</h2>
    <form action="">
        <textarea rows="7" 
                  cols="50" 
                  name="comment" 
                  required>
            </textarea>
        <input type="submit">
    </form>
</body>

</html>

Output: 

Supported Browsers: The browser supported by required attribute are listed below: 



Next Article

Similar Reads