HTML | required Attribute

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

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

  • <input>
  • <select>
  • <textarea>
    <input>: The required attributes can be use in <input> elements.

  • Syntax:
    <input required>
  • Example-1:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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="usrname" required>
                <br>
                Password:
                <input type="password"name="password">
                <br>
                <input type="submit">
            </form>
        </body>
    </html>                    

    chevron_right

    
    

  • Output:
    Image
  • <select>: This elements can also use required attributes.

  • Syntax:
    <select required>
  • Example-2:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>                    

    chevron_right

    
    

  • Output:
    Image
  • <textarea>: This elements can also use required attributes.

  • Syntax:
    <textarea required>
  • Example-3:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <!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>                    

    chevron_right

    
    

  • Output:
    Image

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

  • Google Chrome 5.0+
  • Internet Explorer 11.0+
  • Firefox 4+
  • Opera 9.7+


My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.