Chapter 12 of 26

Input Types

In the chapter Forms we used <input> mainly as a plain text box. But <input> is like a shape-shifter: change one attribute – type – and the same element becomes an email field, a number spinner, a calendar, a colour picker, a slider, a check box or a file chooser.

Picking the right type is not just about looks. It gives mobile users the right keyboard, lets the browser check the value for you, and makes the data arrive at the server in a predictable format. In this chapter we will go through every input type you are likely to use, with the attributes that go with each one.

What is an Input Type?

The type attribute of <input> decides what kind of data the control accepts, how it is displayed, which keyboard or picker the user gets, and what basic validation the browser applies.

<input type="email" name="email">
<input type="number" name="age">
<input type="date" name="dob">

Two rules apply to all of them:

  • If type is missing or the browser does not recognise the value, the input behaves as type="text". This makes newer types safe to use: an old browser simply shows a text box.

  • Whatever the type, the value is always sent to the server as text. A number input sends the characters "25", not a number. The server must convert it.

Here is the full family, grouped by purpose:

Group

Types

Text-like

text, password, email, url, tel, search

Numbers

number, range

Date and time

date, time, datetime-local, month, week

Choices

checkbox, radio

Others

file, color, hidden

Buttons

submit, reset, button, image


Text-like Inputs

text and password

You already know these. text is a single-line box (any line breaks are removed from its value). password masks what is typed. Useful extras:

<label for="user">Username</label>
<input type="text" id="user" name="user" placeholder="e.g. ravi_21"
       maxlength="20" autocomplete="username">

<label for="pwd">Password</label>
<input type="password" id="pwd" name="pwd" autocomplete="current-password">
  • placeholder – a short grey hint shown while the box is empty.

  • maxlength / minlength – character limits (covered in Built-in Form Validation).

  • autocomplete – tells the browser and password managers what the field is (username, current-password, new-password, email, tel, postal-code ...), so they can auto-fill correctly.

email

<label for="mail">Email</label>
<input type="email" id="mail" name="email">

What changes compared to text:

  • Mobile phones show a keyboard with @ and . keys.

  • On submit, the browser checks that the value looks like an email address. ravi@example.com passes; ravi.example.com fails.

  • Leading and trailing spaces are removed automatically.

  • Add multiple to accept a comma-separated list of addresses.

A surprise for many students: ravi@localhost and even a@b are considered valid. The browser only checks the basic syntax "something@something"; a dot in the domain is not required. If you need a stricter rule, add a pattern – and in any case, only sending a confirmation email proves an address is real.

url

<label for="site">Portfolio link</label>
<input type="url" id="site" name="site" placeholder="https://...">

The value must be an absolute URL with a scheme. https://github.com/ravi is valid, but www.github.com is rejected because it has no https:// in front. That is why a placeholder showing https:// is helpful.

tel

<label for="mob">Mobile number</label>
<input type="tel" id="mob" name="mobile" autocomplete="tel">

tel shows a numeric phone keypad on mobiles, but the browser does no format checking at all – even "hello" is accepted. Why? Phone number formats differ hugely between countries (spaces, dashes, +91, brackets), so no single rule would work. If you need a specific format, add pattern, e.g. pattern="[6-9][0-9]{9}" for a 10-digit Indian mobile number.

Why not type="number" for phone numbers? Because a phone number is not a quantity. A number input shows up/down spinner arrows (the value can even change by accident with the arrow keys), rejects +, spaces and dashes, and the server may end up treating it as a number and losing leading zeros. Use number only for values you might add or compare (age, quantity, marks). The same applies to PIN codes, roll numbers and card numbers – use text (with inputmode="numeric", explained below) or tel.

<label for="s">Search courses</label>
<input type="search" id="s" name="q">

It works like text, but browsers may style it differently, often add a small "×" clear button, and some mobile keyboards show a "Search" key instead of "Enter".


Number Inputs

number

<label for="qty">Quantity</label>
<input type="number" id="qty" name="qty" min="1" max="10" step="1" value="1">
  • Only numeric input is accepted; desktop browsers add small up/down arrows (Chrome shows them on hover).

  • min and max set the allowed range.

  • step sets the allowed increments. The default step is 1, so out of the box only whole numbers are valid.

That default step catches many people. Try this:

<input type="number" name="price">

If the user types 2.5 and submits, the browser refuses: 2.5 is not a multiple of the step 1. The fix:

<input type="number" name="price" step="0.01">   <!-- paise allowed -->
<input type="number" name="weight" step="any">   <!-- any decimal -->

Worked example of step with min: allowed values are counted from min (the "step base"; if there is no min, the value attribute is used, and if that is missing too, 0).

<input type="number" name="slot" min="5" max="30" step="5">
Allowed: 5, 10, 15, 20, 25, 30
Typed 12 → invalid (not 5 + a multiple of 5)
Typed 35 → invalid (greater than max)

range

<label for="vol">Volume</label>
<input type="range" id="vol" name="volume" min="0" max="10" step="1">

A slider, for when the exact number matters less than the rough position (volume, brightness, rating). Key facts:

  • Default min is 0, default max is 100.

  • If no value is given, the thumb starts in the middle. The slider above starts at 5; a plain <input type="range"> starts at 50.

  • A range input always has a value, so it can never be "empty".

  • The current number is not shown to the user by default – you need a bit of JavaScript (often with an <output> element) to display it.


Date and Time Inputs

<label for="d">Date of birth</label>
<input type="date" id="d" name="dob" min="1990-01-01" max="2010-12-31">

<label for="t">Preferred time</label>
<input type="time" id="t" name="slot">

<label for="dt">Exam starts at</label>
<input type="datetime-local" id="dt" name="start">

These give the user a native date or time picker instead of making them type a format. The most important thing to understand is the difference between what the user sees and what is submitted:

Type

User picks

Submitted value (always this format)

date

A day

2026-08-15 (yyyy-mm-dd)

time

Hours and minutes

14:30 (24-hour)

datetime-local

Date and time, no time zone

2026-08-15T14:30

month

Month and year

2026-08

week

A week number and year

2026-W33

A user in India may see 15/08/2026 and an American user 08/15/2026 – the display follows the browser's locale – but both send 2026-08-15. Your server code never has to guess the format. That alone is a big reason to use these types.

min and max use the same machine format (for date, yyyy-mm-dd).

Browser support

date, time and datetime-local work in all modern browsers. month and week do not: at the time of writing, MDN lists them as "limited availability" – Chromium browsers (Chrome, Edge, Opera) support them, but desktop Firefox and desktop Safari still show a plain text box. For month or week input that must work everywhere, use two <select> boxes or a date input instead.

(You may also find an old type="datetime" in tutorials. It was removed from the standard and browsers treat it as text. Use datetime-local.)


Choices: Check Boxes and Radio Buttons

checkbox – zero, one or many

A check box is an independent on/off switch. Each box can be ticked separately.

<fieldset>
  <legend>Interests</legend>
  <label><input type="checkbox" name="interest" value="web" checked> Web development</label><br>
  <label><input type="checkbox" name="interest" value="ml"> Machine learning</label><br>
  <label><input type="checkbox" name="interest" value="dsa"> DSA</label>
</fieldset>
<label><input type="checkbox" name="tnc" value="yes"> I accept the terms</label>

If the user ticks "Web development" and "DSA" plus the terms box, the server receives:

interest=web&interest=dsa&tnc=yes
  • checked makes a box ticked initially.

  • The same name on several boxes sends several pairs. (Some server frameworks expect names like interest[] to collect them into a list – that's a server convention, not HTML.)

  • Without a value attribute a ticked box sends on. An unticked box sends nothing at all.

radio – exactly one from a group

Radio buttons come in groups. Choosing one automatically un-chooses the others – just like the preset station buttons on old car radios, which is where the name comes from.

<fieldset>
  <legend>Payment method</legend>
  <label><input type="radio" name="pay" value="upi" checked> UPI</label><br>
  <label><input type="radio" name="pay" value="card"> Debit/Credit card</label><br>
  <label><input type="radio" name="pay" value="cod"> Cash on delivery</label>
</fieldset>

What makes them a group is the shared name. Only the selected one is sent, e.g. pay=card. If no radio in the group is selected, nothing is sent for that name, which is why it's common to pre-select a sensible default with checked (or use required, from the next chapter).

Once a radio in a group is selected, the user cannot unselect all of them by clicking again – another difference from check boxes. Users can also move between options in a group with the arrow keys.

Point

Checkbox

Radio

How many can be chosen

Any number (0 to all)

Exactly one per group

Grouping

Each box independent

Grouped by same name

Can be cleared by clicking again

Yes

No

Typical use

Interests, "I agree", filters

Gender, payment method, Yes/No

Wrapping each group in a <fieldset> with a <legend> (from the Forms chapter) lets screen readers announce the question ("Payment method") along with each option.


Other Useful Types

file

<form action="/upload" method="post" enctype="multipart/form-data">
  <label for="photo">Upload photo (JPG or PNG)</label>
  <input type="file" id="photo" name="photo" accept="image/png, image/jpeg">

  <label for="docs">Certificates (PDF, many allowed)</label>
  <input type="file" id="docs" name="docs" accept=".pdf" multiple>

  <button type="submit">Upload</button>
</form>
  • The form must use method="post" and enctype="multipart/form-data", otherwise only the file name is sent.

  • accept filters the file picker by extension (.pdf) or MIME type (image/png, image/*). It is only a hint: users can still choose other files, so the server must check the real file type and size.

  • multiple allows several files.

  • On phones, capture (e.g. capture="environment" with accept="image/*") can open the camera directly.

  • For security reasons, you cannot set a default file with value.

color

<label for="c">Theme colour</label>
<input type="color" id="c" name="theme" value="#1e90ff">

Opens the system colour picker. In its normal form the value is a lowercase 7-character hex code like #1e90ff (even if you write value="#1E90FF", it reads back as #1e90ff); if you don't set one, the default is #000000 (black). The standard has recently added two optional attributes, alpha (allow transparency) and colorspace, which let the value use other CSS colour notations – but browser support for them is still new and uneven, so the classic hex form is what you will meet almost everywhere.

hidden

<input type="hidden" name="course_id" value="CS301">

Not shown on the page at all, but submitted with the form. It's used to send data the user doesn't need to see – an item ID, the page the user came from, or a security token. Remember: "hidden" only means not displayed. Anyone can see and change it with "View Source" or developer tools, so never put secrets in it or trust it blindly.


Button Types

Type

Behaviour

<input type="submit" value="Send">

Submits the form; text comes from value

<input type="reset">

Resets all fields to initial values

<input type="button" value="Help">

No default action; for JavaScript

<input type="image" src="go.png" alt="Go">

An image that acts as a submit button; also sends the click position as name.x and name.y (or just x and y if it has no name)

As explained in the Forms chapter, the <button> element can do all of this with more flexible content, so prefer it in new code.


Helper Attributes for Inputs

Suggestions with list and <datalist>

A <datalist> gives a text-like input a list of suggestions, while still allowing the user to type anything else.

<label for="college">College</label>
<input type="text" id="college" name="college" list="colleges">
<datalist id="colleges">
  <option value="IIT Bombay"></option>
  <option value="IIT Delhi"></option>
  <option value="NIT Trichy"></option>
  <option value="BITS Pilani"></option>
</datalist>

The input's list attribute must match the <datalist>'s id. As the user types "IIT", matching options appear in a drop-down below the box. Compared with <select>: a select forces a choice from the list; a datalist only suggests.

Choosing the keyboard with inputmode

inputmode tells mobile browsers which virtual keyboard to show without changing validation or behaviour. Values include numeric, decimal, tel, email, url, search and text.

<!-- PIN code: digits keyboard, but still a text field (keeps leading zeros) -->
<label for="pin">PIN code</label>
<input type="text" id="pin" name="pin" inputmode="numeric" maxlength="6" autocomplete="postal-code">

<!-- One-time password from SMS -->
<label for="otp">OTP</label>
<input type="text" id="otp" name="otp" inputmode="numeric" autocomplete="one-time-code">

This combination is the modern recommendation for things made of digits that are not really numbers (OTP, PIN code, card number).


A Demo Page with Many Types

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Input Types Demo</title>
</head>
<body>
  <h1>Hackathon Sign-up</h1>
  <form action="/signup" method="post">
    <p><label for="n">Name</label> <input type="text" id="n" name="name"></p>
    <p><label for="e">Email</label> <input type="email" id="e" name="email"></p>
    <p><label for="m">Mobile</label> <input type="tel" id="m" name="mobile"></p>
    <p><label for="a">Age</label> <input type="number" id="a" name="age" min="16" max="30"></p>
    <p><label for="d">Arrival date</label> <input type="date" id="d" name="arrival"></p>
    <p><label for="s">Skill level</label> <input type="range" id="s" name="skill" min="1" max="5"></p>
    <p><label for="c">Team colour</label> <input type="color" id="c" name="colour" value="#ff6600"></p>
    <fieldset>
      <legend>T-shirt size</legend>
      <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>
    </fieldset>
    <p><label><input type="checkbox" name="veg" value="yes"> Vegetarian meals</label></p>
    <input type="hidden" name="event" value="hack2026">
    <button type="submit">Sign up</button>
  </form>
</body>
</html>

In Chrome this renders as: text boxes for name, email and mobile; a narrower number box for age (Chrome shows its up/down arrows when you hover over it); a date box showing a "dd-mm-yyyy" placeholder with a small calendar icon (the exact format shown depends on your locale); a slider with its thumb in the middle (value 3); an orange colour swatch; a bordered "T-shirt size" group with three radio buttons where M is selected; a "Vegetarian meals" check box; and the Sign up button. The hidden field does not appear anywhere.


Quick Reference

Type

Use it for

Built-in check

Key attributes

text

Short free text

None

maxlength, pattern

password

Secrets

None

autocomplete

email

Email address

Email syntax

multiple

url

Web address

Absolute URL

pattern

tel

Phone number

None

pattern

search

Search box

None

list

number

Quantities

Numeric, range, step

min, max, step

range

Approximate value

Always valid (clamped)

min, max, step

date / time / datetime-local

Dates and times

Valid date/time, range

min, max, step

checkbox

Independent yes/no

None (unless required)

checked, value

radio

One of many

None (unless required)

same name, checked

file

Uploads

None (accept is a hint)

accept, multiple

color

Colour choice

Always a valid colour

value

hidden

Invisible data

None

value


Common Mistakes

  • Using number for phone numbers, PIN codes or card numbers. Use tel or text with inputmode="numeric".

  • Forgetting step for decimals. <input type="number"> rejects 2.5 until you add step="0.01" or step="any".

  • Giving radio buttons different names. Then they are not a group and the user can select all of them.

  • Expecting an unticked check box to send "off". It sends nothing; the server must treat a missing name as "not ticked".

  • Trusting accept or hidden values. Both can be bypassed; validate on the server.

  • Writing dates in min/max/value as 15/08/2026. The browser ignores invalid formats; always use 2026-08-15.

  • Relying on month or week for all users. Firefox and Safari on desktop fall back to plain text.


Interview Questions

1. What happens if a browser doesn't support an input type?

It falls back to type="text". The page keeps working; the user just loses the special picker and validation for that field.

2. Difference between checkbox and radio?

Check boxes are independent – any number can be ticked. Radio buttons sharing the same name form a group where exactly one can be selected, and selecting one deselects the others.

3. A user enters 15/08/2026 in a date picker. What does the server receive?

2026-08-15. Date inputs always submit in yyyy-mm-dd format regardless of how the date is displayed.

4. Why might <input type="number"> reject 9.99?

Because the default step is 1, so only whole steps are valid – counted from min if it is set, otherwise from the value attribute, otherwise from 0. Use step="0.01" or step="any".

5. What does type="tel" validate?

Nothing about the format. It only changes the mobile keyboard and helps auto-fill. Add pattern for format rules.

6. How is <datalist> different from <select>?

A datalist offers suggestions for a text-like input but the user may type any value. A select restricts the user to the listed options.

7. Is type="hidden" a safe place for a price or user ID?

No. Hidden fields are visible in the page source and can be edited before submitting. The server must look up or verify such values itself.


Conclusion

The type attribute turns one element, <input>, into many different controls.

Text-like  → text, password, email, url, tel, search
Numbers    → number (exact), range (slider)
Dates      → date, time, datetime-local (month, week: limited support)
Choices    → checkbox (many), radio (one per name)
Others     → file, color, hidden
Buttons    → submit, reset, button, image
  • Unknown or missing types fall back to text.

  • All values are submitted as text, in fixed machine formats for dates and colours.

  • Pick types by meaning: a phone number is not a number.

  • Use <datalist> for suggestions and inputmode for the right mobile keyboard.

Several of these types already check the value for you. In the next chapter, Built-in Form Validation, we'll see the full set of validation attributes – required, pattern, minlength and more – and how to customise the error messages.