The document introduces 13 new input types in HTML5 for forms: search, email, url, tel, number, range, date, month, week, time, datetime, datetime-local, and color. It provides examples of how to use each input type with descriptions and caveats for some types. It also discusses additional form attributes like autocomplete, autofocus, formaction, and more.
1 of 11
Download to read offline
More Related Content
Html5 forms input types
1. Page 1 of 11
HTML5 formsinputtypes
HTML5 introducesnolessthana bakersdozen(yes,thats13!) new inputtypesforforms.The new
inputtypeswell be lookingatare:
search
email
url
tel
number
range
date
month
week
time
datetime
datetime-local
color
search
<input type="text" name="search">
Well,whatif we couldwrite somethinglike
<input type="search" name="search">
email
<input type="email" name="email" required>
<inputtype="email" name="email"value="xyz.jp">
isntvalidinFirefox,Safari,orChrome (there isnoissue inOpera).
url
<input type="url" name="url" required>
<input type="tel" name="tel" id="tel" required>
number
<inputtype="number"min="5"max="18"step="0.5" value="9"name="shoe-size">
2. Page 2 of 11
<inputtype="text"pattern="[0-9]*"name="shoe-size">
range
<inputid="skill"type="range"min="1"max="100"value="0">
Datesand times
<inputid="dob"name="dob"type="date">
You can go a stepfurtherby usingthe min and max attributestoensure the usercan onlychoose froma
specifieddate range.
<inputid="startdate"name="startdate"min="2012-01-01" max="2013-01-01" type="date">
date
month
<inputid="expiry"name="expiry"type="month"required>
You can alsodrill downto type="week".Notice how Operahighlightsaspecificweekusingthe same
date pickercontrol,
<inputid="vacation"name="vacation"type="week">
time
type="time"rendersaspinbox similartothatusedearlierforselectingthe precisetime.
<inputid="exit-time"name="exit-time"type="time">
datetime
We can combine the date andtime by usingtype="datetime"forspecifyingaprecise time onagiven
day,
<inputid="entry-day-time"name="entry-day-time"type="datetime">
datetime-local
Finallywe canachieve slightlymore granularcontrol byselectingaprecise time ona givendaywitha
local time zone variationusingtype="datetime-local".
<inputid="arrival-time"name="arrival-time "type="datetime-local">
Date andtime caveats
3. Page 3 of 11
.
As withthe othernewinputtypes,if abrowserdoesntrecognizethem, itwill simplydefaultbackto
type="text"
color
<inputid="color"name="color"type="color">
Inputtypessummary
10. Page 10 of 11
Code used in this page
01.<div>
02.<h3>Datalist</h3>
03.<input type="text" name="characters" list="data-list">
04.<datalist id="data-list">
05.<option value="Hugo Reyes">
06.<option value="Jack Shephard">
07.<option value="James 'Sawyer' Ford">
08.<option value="John Locke">
09.<option value="Sayid Jarrah">
10.</datalist>
11.</div>
12.
13.<div>
14.<h3>Keygen</h3>
15.<keygen name="key"></keygen>
16.</div>
17.
18.<div>
19.<h3>Meter</h3>
20.<meter min="0" max="10" value="7"></meter>
21.</div>
22.
23.<div>
24.<h3>Output</h3>
25.<p>If input type="range" and the oninput event on forms are supported in
your web browser, slide the range below to see the value in the output
element.</p>
26.<input type="range" id="range" name="range">
27.<output for="range" id="output"></output>
28.</div>