HTML - attributes - required

revision:


Content

"required" attribute : boolean attribute that specifies what is needed syntax some examples


"required" attribute : boolean attribute that specifies what is needed

top

When present, it specifies that the element must be filled out before submitting the form.

The "required" attribute can be used on the following elements:<input>, <select>, and <textarea>.
The required attribute is supported by "text", "search", "url", "tel", "email", "password", "date", "month", "week", "time", "datetime-local", "number", "checkbox", "radio", "file", <input> types along with the <select> and <textarea> form control elements.

If present on any of these input types and elements, the ":required" pseudo class will match. If the attribute is not included, the ":optional pseudo class" will match.


syntax

top

<element required></element>

The required attribute specifies that the element must be filled out before submitting the form. This attribute is part of the built-in validation functionality in HTML.


some examples

top
codes:
                    <form style="margin-left:3vw;" action="/action_page.php">
                        <label for="username">username:</label>
                        <input type="text" id="username" name="username" required>
                        <input type="submit">
                    </form>
                

The required attribute specifies that the user is required to select a value before submitting the form:



codes
                    <form style="margin-left:3vw;" action="/action_page.php">
                        <label for="cars">Choose a car:</label>
                        <select name="cars" id="cars" required>
                            <option value="">None</option>
                            <option value="volvo">Volvo</option>
                            <option value="saab">Saab</option>
                            <option value="mercedes">Mercedes</option>
                            <option value="audi">Audi</option>
                        </select>
                        <br><br>
                        <input type="submit" value="Submit">
                    </form>