Back to HTML Notes
Topic #155

Text Input

Learn how to create basic text input fields in HTML and understand when to use more specific input types for better user experience.

What it is

The <input> element with type="text" creates a single-line text field where users can enter any characters. It is the most generic form of text entry. While flexible, modern HTML5 offers specialized types like email, password, and number that provide built-in validation and optimized mobile keyboards.

Why it matters

  • User Experience: Specific types trigger appropriate virtual keyboards on mobile devices (e.g., numeric keypad for phone numbers).
  • Data Integrity: Built-in browser validation prevents invalid data submission without JavaScript.
  • Accessibility: Screen readers announce the purpose of the field more accurately with specific types.
  • Simplicity: Reduces the need for custom JavaScript validation logic.

Syntax or steps

To create a standard text input, you need an <input> tag inside a <form>. Always include a <label> associated with the input via the for attribute matching the input's id.

  1. Add the <input> element.
  2. Set type="text" (this is actually the default if omitted).
  3. Provide a unique id for labeling.
  4. Add a name attribute so the data is sent to the server.

Example

<form action="/submit" method="post">
  <!-- Generic Text Input -->
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" placeholder="Enter your username">

  <br><br>

  <!-- Specific Email Input -->
  <label for="user_email">Email:</label>
  <input type="email" id="user_email" name="user_email" required>

  <br><br>

  <button type="submit">Submit</button>
</form>

Explanation: The first input uses type="text" for general alphanumeric data. The second uses type="email", which automatically checks for an "@" symbol and triggers an email-optimized keyboard on mobile. The required attribute ensures the email field cannot be left empty.

Common mistakes

  • Missing Labels: Using only placeholders instead of labels makes forms inaccessible to screen reader users.
  • Overusing Text Type: Using type="text" for emails or passwords loses native validation and security features.
  • No Name Attribute: Without name, the input value is not included in the form submission data.
  • Ignoring Mobile UX: Failing to use type="tel" or type="url" results in full QWERTY keyboards on phones, frustrating users.

When to use it

Use type="text" only when the expected input is truly free-form and does not fit other categories. Compare it with alternatives below:

TypeBest ForKey Benefit
textNames, addresses, commentsAccepts all characters
emailEmail addressesValidates format, mobile keyboard
passwordSecretsMasks characters
numberNumeric valuesSpinners, min/max validation

Practice

Guided Exercise: Create a form with two fields: one for "First Name" using type="text" and one for "Age" using type="number". Ensure both have proper labels.

Challenge: Modify the Age field to accept only integers between 18 and 65 using min and max attributes.

Hint: <input type="number" min="18" max="65">

Quick check

Q: Why should you avoid using type="text" for password fields?

A: Because type="password" masks the input characters for privacy and security, whereas type="text" displays them plainly.

Summary

While type="text" is the default input field, choosing specific HTML5 input types improves accessibility, validation, and mobile usability. Always pair inputs with labels and use the most restrictive type that fits your data requirements.

Want to go beyond the notes?

Join Coding Now Tech Institute's HTML course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Text Input – FAQs

Quick answers about learning Text Input in HTML.

This free note from Coding Now Tech Institute explains Text Input in HTML — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every HTML topic on Coding Now Tech Institute, including Text Input, is 100% free with no signup required.
With focused practice, most students grasp Text Input in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now