CHAPTER 32 · HTML

Input types: choose a field that matches the information

Text, email addresses and passwords can look similar on a page, but their controls have different purposes. Learn the input-type families, then practise useful text fields without pretending that a field alone provides search or login.

Download example files (ZIP)

One element, several kinds of control

An input's type selects the kind of control the browser provides. Think of a paper form with a small box for a name, a tick box for a choice and a date space. On a web page, choosing a suitable type gives the browser more information about the task.

Type is not a promise of identical appearance everywhere. A phone may offer a different keyboard from a desktop computer. A recognised type may also have built-in value checks. If type is omitted or its value is unrecognised, the control generally uses the text state.

A map of the input types

FamilyTypesWhat they are for
Text entrytext, search, email, tel, url, passwordA line of text with a suitable purpose
Numbersnumber, rangeA numeric value or a slider-style numeric choice
Dates and timesdate, time, datetime-local, month, weekStructured date/time values; display support varies
Choicescheckbox, radioIndependent selections or one choice from a group
Other valuescolor, file, hiddenColour selection, local file selection or a non-visible submitted value
Buttonssubmit, reset, button, imageSubmission, resetting, a general button or an image submit control

This chapter focuses on text-entry controls. Chapters 33–35 develop choices, longer answers and native checks. Specialised inputs such as file and colour need their own careful examples in the continuing forms lessons. The table is an overview, not a claim that all of them behave identically.

Text and search

Type="text" is appropriate for a short general answer. Type="search" identifies a search phrase. It may show a clear control or a suitable keyboard action, but it does not build a search engine. The receiving application still needs to use the query.

HTML fragment — part of a document
<label for="query">Topic to find</label>
<input type="search" id="query" name="q" value="HTML lists">

For text-like inputs, value supplies a starting value, size suggests a visible width in characters, and maxlength can limit user-entered length. Size is not a maximum-length rule. Exact length constraints are examined in Chapter 35.

Choose by the information needed
  1. 1Text or search: a general line or query
  2. 2Email and URL: useful native format checks
  3. 3Tel: purpose and possible keypad hints
  4. 4Password: display masking only

An input type does not create search, login or verification services.

Complete program: topic-search.html

Save this file beside form-result.html from Chapter 31.

topic-search.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>A Search Field Practice Page</title>
</head>
<body>
  <h1>Describe a topic search</h1>
  <p>This sends a practice query to the local destination; it does not search a catalogue.</p>
  <form action="form-result.html" method="get">
    <p><label for="query">Topic to find</label></p>
    <p><input type="search" id="query" name="q" value="HTML lists" size="24"></p>
    <button type="submit">Inspect the query</button>
  </form>
</body>
</html>
Browser previewDocument title: A Search Field Practice Page

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. Form submission is disabled in this preview. Save the example and its result page locally to test submission. Use fictional practice values.

Expected result: a search field starts with HTML lists. In a normal saved page, submitting produces a query with key q. The static destination does not return search results. Change the phrase and inspect how spaces are encoded in the address.

Email, URL and telephone

Type="email" asks for an email-shaped value. Type="url" asks for a URL-shaped value. If a non-empty value fails the browser's type check, normal validated submission can be prevented. Neither verifies that an address exists, that a person owns it or that a website is trustworthy.

Type="tel" identifies a telephone entry and may suggest a convenient keypad. It does not impose one universal phone-number format. Phone numbering varies; do not assume tel automatically rejects every incorrect number.

HTML fragment — part of a document
<label for="practice-email">Fictional contact email</label>
<input type="email" id="practice-email" name="email" placeholder="learner@example.com">

Placeholder is a temporary example inside an empty field. It disappears when text is entered and is not a substitute for a label. Put essential instructions in persistent visible text.

Complete program: contact-fields.html

Use made-up values such as learner@example.com. Save form-result.html beside this program to test submission locally.

contact-fields.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Choose Suitable Contact Inputs</title>
</head>
<body>
  <h1>Try fictional contact values</h1>
  <p>Use invented details. This exercise creates no account and stores no contact record.</p>
  <form action="form-result.html" method="get">
    <p><label for="display-name">Practice display name</label></p>
    <p><input type="text" id="display-name" name="display_name" autocomplete="nickname"></p>
    <p><label for="practice-email">Fictional email</label></p>
    <p><input type="email" id="practice-email" name="email" placeholder="learner@example.com"></p>
    <p><label for="practice-site">Example website address</label></p>
    <p><input type="url" id="practice-site" name="website" placeholder="https://example.com"></p>
    <p><label for="practice-phone">Invented phone value</label></p>
    <p><input type="tel" id="practice-phone" name="phone"></p>
    <button type="submit">Inspect these practice values</button>
  </form>
</body>
</html>
Browser previewDocument title: Choose Suitable Contact Inputs

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here. Form submission is disabled in this preview. Save the example and its result page locally to test submission. Use fictional practice values.

Expected result: four labelled fields. Empty optional email and URL fields can be valid; type alone does not make them mandatory. Enter a malformed email value to compare with a well-formed fictional one. We will add required when a value really must be supplied.

Password masking is not encryption

Type="password" usually hides characters visually while someone types. It does not encrypt them, verify them or create a login service. Do not submit real passwords through these local GET examples.

This isolated syntax fragment intentionally has no submission form. Type only invented practice text:

HTML fragment — part of a document
<label for="demo-password">Invented password text for display practice</label>
<input type="password" id="demo-password" autocomplete="new-password">

For a real login field, use appropriate naming and autocomplete="current-password". For creating a password, new-password describes a different purpose. A real authentication system needs receiving-side logic, secure transport and careful storage; masking characters does not supply those features.

Autocomplete and keyboard hints

Autocomplete gives the browser information about autofill purpose. Values such as nickname, email, username and current-password are not interchangeable. A browser may still apply its own behaviour; autocomplete="off" is not an absolute privacy guarantee or a reliable way to defeat a password manager.

Inputmode is a keyboard hint. For example, inputmode="numeric" on a text field may request a numeric keyboard for an identifier. It does not turn the field into a number input or enforce a digit-only rule. A student code with leading zeroes often belongs in a text field because it is an identifier, not a quantity to add or subtract.

Enterkeyhint can suggest a keyboard action label, such as search. It does not implement that action. Introduce only hints that match the actual behaviour of your form.

Hidden fields are still client data

Type="hidden" can include a named value without a visible field. It does not make the value secret or trustworthy. A visitor can inspect and modify client-controlled data. Prices, permissions and trusted account decisions must not depend on believing an unverified hidden value.

Problem and solution: a phone field accepts letters

Problem: a student uses tel and expects a universal telephone validator.

Solution: recognise that tel supplies purpose rather than one international numbering rule. Explain the intended format clearly and use appropriate checks where needed, with receiving-side checks for a real application. Do not reject legitimate formats merely because one example used only digits.

Common mistakes and useful tips

  • Assuming search creates search results or password creates login.
  • Using placeholder as the field's only label.
  • Treating email validity as ownership verification.
  • Using number for every digit-containing identifier.
  • Treating inputmode as validation.
  • Believing hidden means confidential or tamper-proof.
  • Expecting the same native keyboard and decoration on every device.

Practice questions

  1. What happens when an input type is omitted or unrecognised?
  2. Does type="search" create a search service?
  3. What does an email type check fail to prove?
  4. Why is a placeholder not a sufficient field label?
  5. How does inputmode differ from input type?
  6. Save contact-fields.html and its local destination. Try empty optional fields, malformed email text and a fictional well-formed value. Explain which behaviour comes from the browser and which would need an application.
Check your practice answers
  1. The control generally uses the text state.
  2. No. It describes a search-entry control; a receiving application must implement searching.
  3. It does not prove existence, deliverability or ownership.
  4. It disappears after entry and does not replace a persistent accessible name and instructions.
  5. Type selects a control's state and behaviour. Inputmode suggests a keyboard layout without enforcing a value rule.
  6. Empty optional email values can be valid; malformed non-empty values can fail the type check. The local destination still does not verify addresses, save contacts or create an account.

Interview questions

When would a digit-based value still use text input?

When it is an identifier rather than a numeric quantity, such as a student code with meaningful leading zeroes. Choose the type by meaning, not just by the characters it contains.

What does password input protect by itself?

It normally obscures characters from casual viewing on screen. It does not provide encrypted transport, authentication or secure storage.

Can a hidden field be trusted more than a visible field?

No. Both are client-controlled. A receiving service must validate important values and derive trusted decisions from appropriate server-side information.

Do keyboard hints enforce a format?

No. They can improve entry on some devices but do not replace type constraints, explicit validation or clear instructions.

Quick revision

  • Choose input types by the information's purpose.
  • Native checks and keyboards help but do not create application features.
  • Keep visible labels and use placeholders only for supplementary examples.
  • Password masking and hidden fields are not security systems.