On this page12 sections
  1. Learning Objectives
  2. 1. Text-like Types
  3. 2. Numbers
  4. 3. Dates and Times
  5. 4. Choices
  6. 5. Colour and File
  7. 6. Buttons and Hidden Fields
  8. Quick Reference
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 036: Input Types

Category6: Forms
Lecture036 of 048
Estimated video length14 min
TargetHTML5 · any modern browser
LevelBeginner

One element, <input>, becomes a text box, a date picker, a colour picker, a slider or a file chooser, depending only on its type. Choosing the right type gives visitors the right keyboard on their phone and free validation.


Learning Objectives

  • Use every common input type
  • Know which types show special keyboards or pickers on phones
  • Pick the right type for each kind of data

1. Text-like Types

Example: on a phone, each one shows a different keyboard:

html
<form>
  <p><label>Text <input type="text" name="name"></label></p>
  <p><label>Email <input type="email" name="email"></label></p>
  <p><label>Password <input type="password" name="pw"></label></p>
  <p><label>Phone <input type="tel" name="phone" placeholder="0300-1234567"></label></p>
  <p><label>Website <input type="url" name="site" placeholder="https://"></label></p>
  <p><label>Search <input type="search" name="q"></label></p>
</form>
TypeSpecial behaviour
emailChecks for an @; phones show an @ key
passwordHides the characters
telShows a number pad (no format check, because phone formats vary)
urlChecks for a full web address
searchOften adds a ✕ to clear the box

2. Numbers

Example

html
<p><label>Quantity <input type="number" name="qty" min="1" max="10" step="1" value="1"></label></p>
<p><label>Volume <input type="range" name="vol" min="0" max="100" value="40"></label></p>

number gives a box with up/down arrows; range gives a slider. Both use min, max and step.


3. Dates and Times

Example

html
<p><label>Date <input type="date" name="d"></label></p>
<p><label>Time <input type="time" name="t"></label></p>
<p><label>Date and time <input type="datetime-local" name="dt"></label></p>
<p><label>Month <input type="month" name="m"></label></p>
<p><label>Week <input type="week" name="w"></label></p>

Each opens the device's own picker, so visitors can't type an impossible date.


4. Choices

Example

html
<p>Size:
  <label><input type="radio" name="size" value="s"> S</label>
  <label><input type="radio" name="size" value="m" checked> M</label>
  <label><input type="radio" name="size" value="l"> L</label>
</p>
<p><label><input type="checkbox" name="news" checked> Send me the newsletter</label></p>

Radio buttons with the same name allow one choice; checkboxes allow any number.


5. Colour and File

Example

html
<p><label>Favourite colour <input type="color" name="colour" value="#4c6ef5"></label></p>
<p><label>Profile photo <input type="file" name="photo" accept="image/*"></label></p>
<p><label>Documents <input type="file" name="docs" accept=".pdf,.docx" multiple></label></p>

accept limits which files can be chosen; multiple allows several.


6. Buttons and Hidden Fields

html
<input type="submit" value="Send">
<input type="reset" value="Clear">
<input type="button" value="Click me" onclick="alert('Hi')">
<input type="hidden" name="source" value="homepage">

A hidden input sends a value the visitor doesn't see, such as which page the form came from. (Anyone can still read it in the page source, so it's not for secrets.)


Quick Reference

TypeFor
text, password, email, tel, url, searchText
number, rangeNumbers
date, time, datetime-local, month, weekDates and times
checkbox, radioChoices
color, fileColours, uploads
submit, reset, button, imageButtons
hiddenInvisible data

Common Beginner Mistakes

1. type="text" for everything. Visitors miss out on the right keyboard and validation.

2. type="number" for phone numbers or card numbers. Leading zeros disappear and arrows appear. Use tel, or text with inputmode="numeric".

3. Secrets in hidden inputs. They're visible in the source.


Practice Questions

  1. Which type shows a slider?
  2. Which type should you use for a phone number, and why not number?
  3. Write an input that lets visitors choose only image files.
  4. Which types show a date picker?
  5. How do you make only one radio button in a group selectable?
  6. What is a hidden input for?

Practice Answers

  1. range
  2. tel, because number drops leading zeros and adds arrows.
  3. <input type="file" accept="image/*">
  4. date, time, datetime-local, month and week.
  5. Give all of them the same name.
  6. Sending a value with the form that the visitor doesn't need to see.

Key Takeaways

  • type turns <input> into many different controls.
  • The right type means the right phone keyboard and free validation.
  • Dates, colours and files get native pickers.
  • hidden inputs are invisible but not secret.