Loading video player…

Pick the Right Input Type

In this lesson, you gave the work-day sign-up form’s email, phone and hours fields the types that fit their data, and told the browser which saved detail belongs in each field it can fill.

Key Points

  • The type attribute says what kind of data an <input> holds, and the browser picks its tools to match. Like a <button>, an input gets a default when you leave its type out: for an input, that’s type="text", a plain box, which is why every field behaved the same.
  • Where the comparison stops: a type works a little like a variable’s type in a typed language, but a wrong value never throws an error. Some types filter what goes in while someone fills in the field, and the browser can check the value before sending. That check comes later in the course.
  • type="email" looks no different on a laptop. On a phone, the keyboard has an at key on its first page instead of hiding it behind another page of keys.
  • type="tel" brings up a dial pad on a phone. People write phone numbers in many ways, so tel doesn’t check the format at all. Typed on a laptop, it even accepts the words “call Ana”. It only changes the keyboard.
  • type="number" brings up a number keypad on a phone. On a laptop, two small arrows appear inside the field when you point at it, and the up arrow key steps the empty field to 1.
  • Count it or dial it? A number field lets in only what a number can hold: typing abc leaves it empty, and typing 0142 555 gives 0142555, without the space. Phone numbers come with spaces, dashes and plus signs, so use number for things you count and tel for phone numbers.
  • The autocomplete attribute tells the browser which saved detail goes in a field. Without it, Chrome guesses from the field’s name and label. Its value is a token from a fixed list in the HTML standard, so you can’t make one up. The phone field’s name is phone, but its token is tel, because that’s the word on the list. This form needs the tokens name, email and tel. The hours field gets none, because no saved detail fits it.
  • Why a token beats a guess: Chrome might have guessed these three right anyway, but a guess works from the names and labels you picked, and those could be anything. A token means the same to every browser. A visitor picks their saved entry once, and name, email and phone fill in together.
  • More types exist, such as url and date. MDN’s list of input types has them all, and MDN’s autocomplete page lists every token.

Final Code

The first four fields of the sign-up form in events/work-day.html now read:

Language: HTML
<p>
  <label for="name">Your name</label>
  <input id="name" name="name" autocomplete="name">
</p>
<p>
  <label for="email">Email</label>
  <input id="email" name="email" type="email" autocomplete="email">
</p>
<p>
  <label for="phone">Phone (for example, 555 0142)</label>
  <input id="phone" name="phone" type="tel" autocomplete="tel" placeholder="555 0142">
</p>
<p>
  <label for="hours">Hours you can stay</label>
  <input id="hours" name="hours" type="number">
</p>

The task and the time slot are still text boxes. In the next lesson, Group Choices in a Form, you’ll turn them into choices a volunteer can pick.

00:00 Your sign-up form now has a label on every field, including the boxes you added last lesson for the hours, the task and the time slot. But to the browser, every field is still the same plain text box.

00:13 That makes the form slower to fill in. On her phone, Mei taps Email and gets the letter keyboard. The at sign is on another page of keys. She taps Hours, and gets letters again.

00:27 Like a button, an input takes a type attribute, and leaving it out gets you a default. For an input, that’s text, a plain box. That’s why every field behaves the same.

00:39 A type here works a little like a variable’s type in a typed language: it says what kind of data the field holds, and the browser picks its tools to match.

00:48 Here the comparison stops, because a wrong value never throws an error. The browser can check the value before sending, but that comes later. Set the email field’s type to email, and save.

01:02 On your laptop, nothing looks different. On Mei’s phone, the keyboard now has an at key right on its first page.

01:11 For phone numbers, the type is tel, short for telephone. Mei taps Phone and gets a dial pad, the keys she’d use to make a call. People write phone numbers in many ways, with spaces, dashes or a plus sign.

01:26 So tel doesn’t check the format at all. Typed on a laptop, it even accepts the words call Ana as a phone number. It only changes the keyboard.

01:37 Hours is something you count, so its type is number. On Mei’s phone, that brings up a number keypad. On your laptop, point at the field, and two small arrows appear inside it.

01:50 Click into it and press the up arrow key, and the empty field steps to 1.

01:56 So why not number for the phone too? A number field lets in only what a number can hold. Type a, b, c, and nothing goes in. Type 0142, space, 555, and the space never arrives.

02:12 Phone numbers come with spaces, dashes and plus signs. So use number for things you count, and tel for phone numbers.

02:21 On her laptop, Mei’s Chrome also has her name, email and phone saved. To fill them in, it has to know which field wants which. Without help, Chrome guesses from each field’s name and label.

02:35 The autocomplete attribute says it outright. Its value is a token from a fixed list in the HTML standard, so you can’t make one up. This form needs the tokens name, email and tel, each on the field it describes.

02:49 The phone field’s name is phone, but its token is tel, because that’s the word on the list. Hours goes without one, because no saved detail fits it. Chrome might have guessed these three right anyway.

03:02 But a guess works from the names and labels you picked, and those could be anything. A token means the same to every browser. Now Mei clicks Your name. Chrome lists her saved details.

03:13 She picks the entry, and name, email and phone fill in at once.

03:19 So the type says what kind of data a field holds, and autocomplete says which saved detail belongs in it. From those, the browser picks the keyboard and the saved details.

03:30 There are more types, such as URL and date, and MDN lists them all, along with every autocomplete token.

03:38 The task and the time slot are still text boxes, though. The next lesson, Group Choices in a Form, turns them into choices a volunteer can pick.

Become a Member to join the conversation.