English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In HTML5In HTML, multiple new form input types have been added. By using these new elements, better input control and validation can be achieved.
HTML5 It has multiple new form input types. These new features provide better input control and validation.
This chapter provides a comprehensive introduction to these new input types:
color
date
datetime
datetime-local
month
number
range
search
tel
time
url
week
Note:Not all mainstream browsers support the new input types, but you can already use them in all mainstream browsers. Even if they are not supported, they can still be displayed as a conventional text field.
The color type is used in input fields mainly for selecting colors, as shown below:
Select a color from the color picker:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> Choose your favorite color: <input type="color" name="favcolor"><br> <input type="submit"> </form> </body> </html>Test and see ‹/›
The date type allows you to select a date from a date picker.
Define a time controller:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> Birthday: <input type="date" name="bday"> <input type="submit"> </form> </body> </html>Test and see ‹/›
The datetime type allows you to select a date (UTC time).
Define a date and time controller (local time):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> Birthday (date and time): <input type="datetime" name="bdaytime"> <input type="submit"> </form> </body> </html>Test and see ‹/›
datetime-The local type allows you to select a date and time (without timezone).
Define a date and time (without timezone):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> Birthday (date and time): <input type="datetime-local" name="bdaytime"> <input type="submit"> </form> </body> </html>Test and see ‹/›
The email type is used for input fields that should contain e-email input field.
When submitting the form, the value of the email field will be automatically verified to ensure it is valid and legal:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> E-mail: <input type="email" name="usremail"> <input type="submit"> </form> <p><b>Attention:</b> Internet Explorer 9 and earlier IE versions do not support type="email" .</p> </body> </html>Test and see ‹/›
Hint: The Safari browser in iPhone supports email input type and coordinates with it by changing the touch screen keyboard (adding @ and .com options).
The month type allows you to select a month.
Define month and year (without timezone):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> Birthday (month and year): <input type="month" name="bdaymonth"> <input type="submit"> </form> </body> </html>Test and see ‹/›
The number type is used for input fields that should contain numbers.
You can also set restrictions on the accepted numbers:
Define a numeric input field (restricted):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> quantity ( 1 to 5 between): <input type="number" name="quantity" min="1" max="5"> <input type="submit"> </form> <p><b>Attention:</b>Internet Explorer 9 and earlier IE versions do not support type="number" .</p> </body> </html>Test and see ‹/›
Use the following attributes to specify restrictions on numeric types:
attribute | Description |
disabled | The input field is disabled |
max | Specify the maximum value allowed |
maxlength | Specify the maximum character length of the input field |
min | Specify the minimum value allowed |
pattern | Specify the pattern used to validate the input field |
readonly | Specify that the value of the input field cannot be modified |
required | Specify that the value of the input field is required |
size | Specify the number of visible characters in the input field |
step | Specify the valid numeric interval of the input field |
value | Specify the default value of the input field |
The range type is used for input fields that should contain numeric values within a certain range.
The range type is displayed as a slider.
Define a non-precise numeric value that is not very accurate (similar to slider control):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php" method="get"> Points: <input type="range" name="points" min="1" max="10"> <input type="submit"> </form> <p><b>Attention:</b> Internet Explorer 9 Early versions of IE do not support type="range" .</p> </body> </html>Test and see ‹/›
Use the following properties to specify limits for numeric types:
max - Specify the maximum value allowed
min - Specify the minimum value allowed
step - Specify the valid numeric interval
value - Specify the default value
The search type is used for search fields, such as site search or Google search.
Define a search field (similar to site search or Google search):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> Search Google: <input type="search" name="googlesearch"><br> <input type="submit"> </form> </body> </html>Test and see ‹/›
Define input phone number field:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> Phone number: <input type="tel" name="usrtel"><br> <input type="submit"> </form> </body> </html>Test and see ‹/›
The time type allows you to select a time.
Define an input time controller (without time zone):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> Select time: <input type="time" name="usr_time"> <input type="submit"> </form> </body> </html>Test and see ‹/›
The URL type is used for input fields that should contain URL addresses.
The value of the URL domain will be automatically verified when the form is submitted.
Define input URL field:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> Add your homepage: <input type="url" name="homepage"><br> <input type="submit"> </form> <p><b>Attention:</b> Internet Explorer 9Early versions of IE do not support type="url" .</p> </body> </html>Test and see ‹/›
Hint: The Safari browser in iPhone supports URL input type and coordinates with it by changing the touch screen keyboard (adding the .com option).
The week type allows you to select week and year.
Define week and year (no timezone):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Basic Tutorial(oldtoolbag.com)</title> </head> <body> <form action="demo-form.php"> Select week: <input type="week" name="year_week"> <input type="submit"> </form> </body> </html>Test and see ‹/›
Label | Description |
<input> | Description of the input field |