HTML input attributes fine-tune what users can enter into a field and how browsers validate data before submission. Using native constraint attributes reduces custom JavaScript validation code while making forms clearer for keyboard and screen reader users.
required: Prevents form submission when the field is left empty.min, max, and step: Bound numeric and date inputs (for example, min="18" max="99").minlength and maxlength: Enforce minimum and maximum character counts in text fields.pattern: Validates text input against a regular expression (pair it with a title describing the required format).readonly vs. disabled: Both block user edits, but readonly keeps the field focusable and submits its value, whereas disabled grays out the field and excludes it from form submission.placeholder and autocomplete: Show brief format hints and enable browser autofill (autocomplete="email").<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Input Attributes Demo</title>
<style>
form { max-width: 420px; font-family: system-ui, sans-serif; display: grid; gap: 12px; }
input { padding: 8px; width: 100%; box-sizing: border-box; }
</style>
</head>
<body>
<form>
<div>
<label for="order-ref">Order Reference (Read-only)</label>
<input id="order-ref" name="order_ref" type="text" value="ORD-2026-884" readonly>
</div>
<div>
<label for="invite-code">6-Character Promo Code</label>
<input
id="invite-code"
name="promo_code"
type="text"
pattern="[A-Z0-9]{6}"
maxlength="6"
title="Enter 6 uppercase letters or digits (e.g. SAVE20)"
required
>
</div>
<button type="submit">Apply Code</button>
</form>
</body>
</html>
disabled on fields the backend still needs: Disabled values are omitted from POST and GET payloads. Use readonly when the server still expects the value.autofocus carelessly on mid-page forms: autofocus jumps the scroll position down immediately on page load, disorienting screen reader users. Reserve it for dedicated login or search pages.pattern without a helpful title: Generic "Please match the requested format" alerts confuse users unless title explains the exact rule.readonly allows keyboard focus and sends the field value when the form submits, while disabled skips tab focus and omits the value from submission.
No. Always validate string length on the backend as well because anyone can modify HTML attributes in browser DevTools.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.