YouTip LogoYouTip

Att Input Required

## HTML required Attribute The `required` attribute is a boolean attribute used in HTML `` elements. When present, it specifies that an input field must be filled out (or a value must be selected) before the user can submit the form. --- ## Syntax In HTML, you can write the `required` attribute in its minimized form: ```html ``` ### XHTML Difference In XHTML, attribute minimization is forbidden. The `required` attribute must be defined with its value explicitly declared: ```html ``` --- ## Code Example Below is an example of an HTML form with a required text input field. If you try to submit this form without entering a username, the browser will block the submission and display a validation message. ```html
``` --- ## Supported Input Types The `required` attribute does not apply to all input types. It is designed to work with the following `` types: * `text` * `search` * `url` * `tel` * `email` * `password` * Date pickers (e.g., `date`, `datetime-local`, `month`, `time`, `week`) * `number` * `checkbox` * `radio` * `file` --- ## Browser Support The numbers in the table below indicate the first browser version that fully supports the `required` attribute. | Attribute | Chrome | Edge / IE | Firefox | Safari | Opera | | :--- | :--- | :--- | :--- | :--- | :--- | | **required** | 5.0 | 10.0 | 4.0 | 5.0 | 9.6 | --- ## Key Considerations & Best Practices ### 1. Styling Required Fields You can use the CSS `:required` pseudo-class to style input elements that have the `required` attribute, and the `:invalid` pseudo-class to style them when they fail validation. ```css /* Style required inputs with a red border when they are empty/invalid */ input:required:invalid { border: 2px solid red; } /* Style required inputs with a green border when they are valid */ input:required:valid { border: 2px solid green; } ``` ### 2. Accessibility (a11y) While modern screen readers generally support the HTML5 `required` attribute, it is a best practice to also include `aria-required="true"` to ensure maximum compatibility with assistive technologies: ```html ``` ### 3. Bypassing Validation If you need to submit a form without validating required fields (for example, a "Save Draft" button), you can add the `formnovalidate` attribute to the submit button: ```html
``` ### 4. Server-Side Validation **Important:** Client-side validation using the `required` attribute is excellent for user experience, but it can be easily bypassed by malicious users or disabled in browser developer tools. Always validate user input on the server side as well.
← Att Input SizeAtt Input Readonly β†’