HTML5 Forms
HTML5 greatly enhanced form functionality by introducing many new input types, attributes, and elements, aimed at providing better user experience, stronger data validation, and reducing JavaScript dependency.
New <input> Types
HTML5 added multiple new type attribute values, allowing browsers to better understand the input field's purpose and provide corresponding UI (like date pickers) and validation.
-
email: For entering email addresses. The browser automatically validates if the input matches standard email format. -
url: For entering URLs. The browser validates the format. -
number: For entering numbers. You can setmin,max,step(step size), and other attributes. On mobile devices, it usually shows a numeric keyboard. -
date: Provides a date picker for selecting year, month, and day. -
time: Provides a time picker. -
color: Provides a color picker. -
range: Creates a slider control for selecting a value within a specified range. -
search: For search fields, which may appear similar totextbut functionally may have a clear button.
New Form Attributes
These new attributes can be applied to <input>, <form>, <textarea>, and other elements, providing powerful declarative validation and interaction features.
-
placeholder: Displays a short hint when the input field is empty. The text automatically disappears when the user starts typing. -
required: Specifies that the input field must be filled before submitting. If empty, the browser will prevent form submission and prompt the user. -
autofocus: When the page loads, automatically sets focus to this input field. -
pattern: Provides a regular expression to validate the input field value. If the value doesn't match the pattern, the form cannot be submitted. -
multiple: Allows users to select multiple files in<input type="file">, or enter multiple comma-separated email addresses in<input type="email">. -
formaction,formmethod, etc.: Allow submit buttons (<input type="submit">or<button>) to override theactionandmethodattributes of their parent<form>, enabling multiple buttons with different submission behaviors within one form.
The <datalist> Element
The <datalist> element provides a predefined list of options for input fields (autocomplete functionality). Users can either select from the list or enter their own value.
By setting the <input>'s list attribute to the <datalist>'s id, you can associate them.