← Back to HTML Course | Chapter 5: Forms | Lesson 5 of 7

HTML Input Attributes

Imagine walking up to a soda vending machine. Some buttons have prices labeled underneath (placeholders), some are completely locked out of order (disabled), and some require you to insert exact coins before they work (required). HTML input attributes are those machine settings. They do not change what type of field is displayed on your page, but they control how that field behaves, what data it accepts, and how users interact with it. Configuring these attributes is essential for guiding users through your forms and preventing input errors.
Syntax
markup
<input type="type" name="name" value="value" placeholder="hint" required>

The value and placeholder Attributes

The value attribute sets the default, pre-populated text inside an input field when the page loads.

The placeholder attribute displays a faint, temporary hint inside the field to guide the user on what to type, disappearing as soon as they start entering text.

Note:
  • Use placeholders to show formatting examples, like '[email protected]' on email fields.
  • Accepted attributes:
  • value — initial or submitted value
  • placeholder — hint text shown while empty
  • Placeholder does not replace a label
Warning: Never use placeholders as a replacement for actual label elements, as screen readers may skip them.

Example: The value and placeholder Attributes

markup
<input type="text" value="Default text">
<input type="email" placeholder="[email protected]">

The readonly and disabled Attributes

The readonly attribute allows users to see and select the text inside a field but prevents them from editing it, still sending the data to the server on submission.

The disabled attribute completely locks the element, graying it out and preventing its data from being submitted.

Note:
  • Use the readonly attribute to display data (like order numbers) that you want users to copy but not change.
  • Accepted attributes:
  • readonly — boolean; value is submitted, not editable
  • disabled — boolean; not editable, not submitted, not focusable
Warning: Disabled input fields are completely ignored during form submission, so their data is not sent to the server.

Example: The readonly and disabled Attributes

markup
<input type="text" value="Order #1234" readonly>
<input type="text" value="Cannot edit" disabled>

The required and autofocus Attributes

The required attribute tells the browser that a field must be filled out before the form can be submitted, displaying a native error message if left blank.

The autofocus attribute automatically places the cursor inside a specific field as soon as the page loads.

Note:
  • Only use autofocus on one single input field per page to keep page navigation smooth.
  • Accepted attributes:
  • required — boolean; must be filled before submit
  • autofocus — boolean; focus on page load (one per page)
Warning: Applying autofocus to fields located lower on the page can cause the browser to jump past your header content on load.

Example: The required and autofocus Attributes

markup
<input type="text" required autofocus>

Character Limits with minlength and maxlength

The minlength attribute sets the minimum number of characters a user must type in a field, and the maxlength attribute sets the maximum limit, preventing them from entering any characters past that threshold.

Note:
  • Use maxlength on passcode fields to enforce secure, standard character counts.
  • Accepted attributes:
  • minlength — non-negative integer
  • maxlength — non-negative integer
  • Apply to text, search, url, tel, email, password and textarea
Warning: Ensure your character limits align with your database settings to prevent data truncation errors.

Example: Character Limits with minlength and maxlength

markup
<input type="text" minlength="4" maxlength="8">

Custom Validation with pattern and title

The pattern attribute allows you to write custom validation rules using Regular Expressions (regex) to check that user inputs match specific formatting structures (like zip codes or phone numbers), while the title attribute displays a custom error tooltip if the validation fails.

Note:
  • Use pattern attributes to enforce secure, custom input formats without needing complex JavaScript scripts.
  • Accepted attributes:
  • pattern — regular expression matched against the full value
  • title — text explaining the required format
  • Applies to text, search, url, tel, email, password
Warning: Always write clear title tooltips to explain what formatting errors caused the validation to fail.

Example: Custom Validation with pattern and title

markup
<input type="text" pattern="[0-9]{5}" title="Enter a 5-digit zip code">
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using placeholders as a replacement for actual label tags, causing web accessibility violations.
  2. Expecting disabled input fields to submit their data to the server.
  3. Forgetting to include the hashtag symbol (#) at the start of your hex color inputs.
Chapter Summary
  • Attributes control how input fields behave, what data they accept, and how users interact with them.
  • Use placeholder to display formatting hints, required to make fields mandatory, and value to pre-populate default text.
  • Use readonly and disabled attributes to restrict inputs, and minlength, maxlength, or pattern to enforce formatting rules.
Browser Support

Standard input attributes are fully supported by all modern and legacy web browsers.

🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.