IntermediateLesson 12 of 307 min read

HTML input types: text, email, number, date and more

Meet the many types of the input element and learn which one to use for emails, phones, numbers, dates, files and colours.

In this lesson you will

  • Choose the right input type for each question
  • Limit numbers and dates with min and max
  • Upload files from a form
  • Use placeholder and autocomplete properly

About 13 minutes

One tag, many costumes

Picture the sign-up page of a gym. It asks for an email address, a phone number, a birth date, a preferred start time and a password. If every answer went into the same plain text box, people on phones would be typing digits on a letter keyboard and mistyping dates all day. HTML solves this with a single element wearing many costumes: <input>, whose type attribute changes how it looks and behaves.

The everyday types

<input type="text" name="name">
<input type="email" name="email">
<input type="password" name="pass">
<input type="tel" name="phone">
<input type="url" name="website">
<input type="number" name="guests" min="1" max="20">
  • text is the default. One line of free typing.
  • email expects something shaped like an address. On phones it shows a keyboard with the @ key ready.
  • password hides the characters as dots.
  • tel shows the phone keypad on a mobile screen. It does not check the format, since phone numbers vary worldwide.
  • url expects a web address beginning with a scheme such as https.
  • number allows only numbers and adds little up and down arrows. You can limit it with min, max and step.

Choosing the correct type costs nothing and gives you three gifts for free: a better keyboard on phones, basic checking by the browser, and clearer meaning for assistive technology.

Change each input to a better type: the first should be an email, the second a phone number, the third a number between 1 and 10.

<label>Email <input type="text" name="email"></label><br>
<label>Phone <input type="text" name="phone"></label><br>
<label>Guests <input type="text" name="guests"></label>

Replace the type values with email, tel and number. For the last one add min=”1″ and max=”10″.

<label>Email <input type="email" name="email"></label><br>
<label>Phone <input type="tel" name="phone"></label><br>
<label>Guests <input type="number" name="guests" min="1" max="10"></label>
Good to know

Notice that here the input sits inside the label. That is a second valid way to connect them, and it needs no for or id. Choose whichever you find easier to read.

Dates and times

Typing a date by hand invites mistakes: is 03/04 the third of April or the fourth of March? Date inputs show a calendar picker and send the value in a fixed, unambiguous format.

<input type="date" name="start">
<input type="time" name="class-time">
<input type="datetime-local" name="booking">
<input type="month" name="card-month">
<input type="week" name="week">

You can restrict choices with min and max. For a booking form you might block past dates: min="2026-11-01". The exact look of the picker differs between browsers and phones, but the value that arrives is always in the same order: year, month, day.

Choosing with sliders and colours

<label>Volume <input type="range" name="volume" min="0" max="100" value="40"></label>
<label>Favourite colour <input type="color" name="colour" value="#5A28C8"></label>

A range gives a slider. It suits settings where the exact number matters less than the position. A color input opens the system colour picker and sends a value such as #5A28C8.

Files and hidden values

A file input lets visitors choose a document or photo from their device. Forms that upload files also need an extra attribute: enctype="multipart/form-data" on the form, and the method must be post.

<form action="/upload" method="post" enctype="multipart/form-data">
  <label for="cv">Upload your CV</label>
  <input type="file" id="cv" name="cv" accept=".pdf,.docx">
  <button type="submit">Upload</button>
</form>

The accept attribute suggests which kinds of files to show in the picker. It is a convenience, not a security measure, because the server must still check what arrives.

A hidden input sends a value that visitors never see, such as the name of the page the form came from. It is useful, but never put secrets there: anyone can read it by viewing the page source.

Helpful attributes for any input

  • placeholder shows a faint example inside the empty box, such as name@example.com. It is a hint, not a label.
  • value sets what is already in the box when the page loads.
  • autocomplete helps browsers fill in known details, for example autocomplete="email". People love forms that fill themselves.
  • readonly shows a value that cannot be edited but is still sent. disabled greys out a control and does not send it.
  • autofocus puts the cursor in the box when the page opens. Use it sparingly.
Let the browser do the typing

Adding autocomplete="name", "email" and "tel" to a checkout form can cut the time to complete it from minutes to seconds on a phone.

Build a small booking form with a name (text), an email, a date input and a number input for guests from 1 to 8. Add a placeholder to the name box.

<form>
  <!-- build the four fields with labels -->
</form>

Wrap each input in a label. Use type=”date” and type=”number” min=”1″ max=”8″.

<form>
  <label>Name <input type="text" name="name" placeholder="Your full name"></label><br>
  <label>Email <input type="email" name="email"></label><br>
  <label>Date <input type="date" name="date"></label><br>
  <label>Guests <input type="number" name="guests" min="1" max="8"></label>
</form>
Common mistake

Do not use type="number" for things that merely look like numbers but are not quantities, such as phone numbers, postcodes or card numbers. People never add them up, leading zeros can vanish, and the little arrows make no sense. Use text or tel with a suitable inputmode instead.

Choosing the right type: three quick scenarios

Let us practise the decision itself, because that is the real skill. Imagine you are building three forms in one week. The first is a newsletter signup that asks only for an email address. You choose type="email", add required and autocomplete="email", and a visitor on a phone gets the right keyboard and a one-tap autofill. The second is a ticket booking form where people pick a date no earlier than tomorrow and a number of seats between one and six. Here you reach for type="date" with a min, and type="number" with min and max. The third is a profile page with a link to someone’s portfolio. That is a url input, and the browser will refuse a value that is not a web address.

Notice the pattern: you never ask “which input looks right?” but “what kind of information is this?” When the answer names a kind, such as email, phone, date or amount, HTML almost certainly has a type that matches it.

Debugging a stubborn input

  • The field accepts anything. Check the spelling of the type. Browsers silently treat an unknown type, such as type="emial", as plain text.
  • The picker looks different on every device. That is expected. Each browser draws its own date and colour pickers, and you cannot restyle them fully. The value that arrives is the same everywhere.
  • The number arrows are annoying. If people never click them, you probably chose the wrong type. Use text with a suitable pattern instead.
  • The autofill is wrong. Make sure each autocomplete value matches the question, for example given-name for a first name and postal-code for a postcode.

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

Which input type brings up the @ key on a phone keyboard?

  1. text
  2. email
  3. tel
  4. url

The email type tells the phone to show an email-friendly keyboard.

Which attribute limits a number input to at most 20?

  1. limit="20"
  2. max="20"
  3. top="20"
  4. size="20"

min and max set the smallest and largest allowed values.

What must be added to a form that uploads files?

  1. method="get"
  2. enctype="multipart/form-data" with post
  3. A hidden input
  4. A fieldset

File uploads need post and the multipart encoding.

What is placeholder text for?

  1. Replacing the label
  2. A short example hint inside an empty box
  3. Hiding the box
  4. Making the field required

It is only a hint. A real label is still needed.

Which type is best for a phone number?

  1. number
  2. tel
  3. date
  4. range

tel shows the keypad without turning the value into a quantity.

Summary

  • The type attribute changes the keyboard, the checking and the meaning of an input.
  • Use email, tel, url, number and date instead of plain text where they fit.
  • File uploads need post and multipart encoding.
  • Placeholders are hints, not labels.

Next lesson

Not every question fits in a single line. Next: text areas, drop-down lists, checkboxes and radio buttons.

Questions

What if a browser does not support a type?

It falls back to a plain text box, so the form still works. That is why choosing a specific type is safe.

Does type=email guarantee a real address?

No. It only checks the general shape, such as the presence of an @ sign. A real check means sending a confirmation email.

Should I use type=number for ages?

Yes, an age is a quantity, so number with min and max is a good fit.

Progress is saved in this browser.