On this page12 sections
Lecture 036: Input Types
| Category | 6: Forms |
| Lecture | 036 of 048 |
| Estimated video length | 14 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
One element,
<input>, becomes a text box, a date picker, a colour picker, a slider or a file chooser, depending only on itstype. 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:
<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>| Type | Special behaviour |
|---|---|
email | Checks for an @; phones show an @ key |
password | Hides the characters |
tel | Shows a number pad (no format check, because phone formats vary) |
url | Checks for a full web address |
search | Often adds a ✕ to clear the box |
2. Numbers
Example
<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
<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
<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
<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
<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
| Type | For |
|---|---|
text, password, email, tel, url, search | Text |
number, range | Numbers |
date, time, datetime-local, month, week | Dates and times |
checkbox, radio | Choices |
color, file | Colours, uploads |
submit, reset, button, image | Buttons |
hidden | Invisible 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
- Which type shows a slider?
- Which type should you use for a phone number, and why not
number? - Write an input that lets visitors choose only image files.
- Which types show a date picker?
- How do you make only one radio button in a group selectable?
- What is a hidden input for?
Practice Answers
rangetel, becausenumberdrops leading zeros and adds arrows.<input type="file" accept="image/*">date,time,datetime-local,monthandweek.- Give all of them the same
name. - Sending a value with the form that the visitor doesn't need to see.
Key Takeaways
typeturns<input>into many different controls.- The right type means the right phone keyboard and free validation.
- Dates, colours and files get native pickers.
hiddeninputs are invisible but not secret.