New HTML5 Input Types
HTML5 has several new form input types. These new features provide better input control and validation.
This chapter gives a comprehensive introduction to these new input types:
- color
- date
- datetime
- datetime-local
- month
- number
- range
- search
- tel
- time
- url
- week
**Note:** Not all major browsers support the new input types, but you can already use them in all major browsers. Even if they are not supported, they will still be displayed as regular text fields.
Input Types: color
The color type is used in input fields mainly for picking colors, as shown below:
Example
Choose a color from the color picker:
Choose your favorite color:
Try it »
Input Types: date
The date type allows you to select a date from a date picker.
Example
Define a time control:
Birthday:
Try it »
Input Types: datetime
The datetime type allows you to select a date (UTC time).
Example
Define a date and time control (local time):
Birthday (date and time):
Try it »
Input Types: datetime-local
The datetime-local type allows you to select a date and time (no time zone).
Example
Define a date and time (no time zone):
Birthday (date and time):
Try it »
Input Types: email
The email type is used for input fields that should contain an e-mail address.
Example
When the form is submitted, the value of the email field is automatically validated to ensure it is valid:
E-mail:
Try it »
Tip: The Safari browser on the iPhone supports the email input type and adapts to it by changing the touch-screen keyboard (adding @ and .com options).
Input Types: month
The month type allows you to select a month.
Example
Define a month and year (no time zone):
Birthday (month and year):
Try it »
Input Types: number
The number type is used for input fields that should contain a numeric value.
You can also set restrictions on the accepted numbers:
Example
Define a numeric input field (with restrictions):
Quantity (between 1 and 5):
Try it »
Use the following attributes to specify restrictions on the number type:
| property | Description |
|---|---|
| disabled | Specifies that the input field is disabled |
| max | Specifies the maximum value allowed |
| maxlength | Specifies the maximum number of characters allowed in the input field |
| min | Specifies the minimum value allowed |
| pattern | Specifies the pattern used to validate the input field |
| readonly | Specifies that the value of the input field cannot be modified |
| required | Specifies that the value of the input field is required |
| size | Specifies the number of visible characters in the input field |
| step | Specifies the legal number interval of the input field |
| value | Specifies the default value of the input field |
Try an example with all the restriction attributes Try it
Input Types: range
The range type is used for input fields that should contain a numeric value within a certain range.
The range type is displayed as a slider.
Example
Define a value that does not need to be very precise (similar to a slider control):
Try it »
Use the following attributes to specify restrictions on the number type:
- max - specifies the maximum allowed value
- min - specifies the minimum allowed value
- step - specifies the legal number intervals
- value - specifies the default value
Input Types: search
The search type is used for search fields, such as site search or Google search.
Example
Define a search field (similar to site search or Google search):
Search Google:
Try it »
Input Types: tel
Example
Define an input field for a telephone number:
Telephone number:
Try it »
Input Types: time
The time type allows you to select a time.
Example
Define a time input control (no time zone):
Select a time:
Try it »
Input Types: url
The url type is used for input fields that should contain a URL address.
When the form is submitted, the value of the url field is automatically validated.
Example
Define an input field for a URL:
Add your homepage:
Try it »
Tip: The Safari browser on the iPhone supports the url input type and adapts to it by changing the touch-screen keyboard (adding the .com option).
Input Types: week
The week type allows you to select a week and a year.
Example
Define a week and a year (no time zone):
Select a week:
Try it »
HTML5 tag
| tag | Description |
|---|---|
| Describes the input element |