- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Code Builder
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
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.
To Continue Learning Please Login
Login with Google