On this page11 sections
  1. Learning Objectives
  2. 1. Starting Values and Hints
  3. 2. Read-only vs Disabled
  4. 3. Size and Length
  5. 4. Validation: required, min, max, step, pattern
  6. 5. multiple, list and inputmode
  7. 6. Form Attributes on Inputs and Buttons
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 037: Input Attributes

Category6: Forms
Lecture037 of 048
Estimated video length14 min
TargetHTML5 · any modern browser
LevelBeginner

Attributes on an <input> set its starting value, size and limits, make it required or read-only, add hints, and check its format. A separate set, the form attributes, let an input or button override the settings of its form.


Learning Objectives

  • Use value, placeholder, readonly, disabled, size, maxlength and autofocus
  • Validate with required, min, max, step and pattern
  • Use multiple, list and inputmode
  • Override form settings with form, formaction, formmethod and formnovalidate

1. Starting Values and Hints

Example

html
<p><label>City <input name="city" value="Lahore"></label></p>
<p><label>Email <input type="email" name="email" placeholder="you@example.com"></label></p>
<p><label>Search <input type="search" name="q" autofocus></label></p>
  • value: what's already in the box
  • placeholder: a hint that disappears as you type (not a replacement for a label)
  • autofocus: puts the cursor here when the page loads (use once per page)

2. Read-only vs Disabled

Example

html
<form>
  <p><label>Order number <input name="order" value="A-1042" readonly></label></p>
  <p><label>Coupon <input name="coupon" value="EXPIRED" disabled></label></p>
  <button type="submit">Submit</button>
</form>
Can edit?Can focus?Sent with the form?
readonlyNoYesYes
disabledNoNoNo

Submit the example to see that coupon isn't sent.


3. Size and Length

Example

html
<p><label>PIN <input name="pin" size="4" maxlength="4" minlength="4"></label></p>
  • size: visible width, in characters
  • maxlength / minlength: allowed number of characters

4. Validation: required, min, max, step, pattern

Example: try submitting with wrong values:

html
<form>
  <p><label>Name (required) <input name="name" required></label></p>
  <p><label>Age 18–60 <input type="number" name="age" min="18" max="60"></label></p>
  <p><label>Price (steps of 50) <input type="number" name="price" step="50" value="100"></label></p>
  <p><label>Postcode (5 digits) <input name="postcode" pattern="[0-9]{5}" title="Five digits, e.g. 54000"></label></p>
  <button type="submit">Check</button>
</form>

pattern uses a regular expression. [0-9]{5} means "exactly five digits". The title text is shown in the error message, so explain the format there.


5. multiple, list and inputmode

html
<p><label>Emails (comma-separated) <input type="email" name="to" multiple></label></p>
<p><label>Card number <input name="card" inputmode="numeric" autocomplete="cc-number"></label></p>
  • multiple: several values (emails or files)
  • list: connects to a <datalist> (Lecture 035)
  • inputmode: which phone keyboard to show (numeric, decimal, email, tel…) without changing the type

6. Form Attributes on Inputs and Buttons

These let an element override its form, or sit outside it.

Example: two buttons, one form, two behaviours:

html
<form action="/save" method="post">
  <input name="title" value="My draft" required>
  <button type="submit">Publish</button>
  <button type="submit" formaction="/drafts" formmethod="get" formnovalidate>Save draft</button>
</form>
AttributeOverrides the form's…
formactionaction
formmethodmethod
formtargettarget
formenctypeenctype
formnovalidateValidation (skip it for this button)

An input can even live outside its form with the form attribute:

html
<form id="newsletter" action="/subscribe"></form>
<input name="email" form="newsletter" placeholder="Email (outside the form)">
<button type="submit" form="newsletter">Subscribe</button>

Common Beginner Mistakes

1. Placeholder as the only label. It disappears as soon as you type.

2. Expecting disabled fields to be sent. They aren't. Use readonly to send a fixed value.

3. pattern without title. Visitors don't know what format you want.

4. autofocus on several fields. Only the first one wins, and it can disorient screen-reader users.


Practice Questions

  1. What's the difference between value and placeholder?
  2. A field must be sent but not edited. readonly or disabled?
  3. Write an input that accepts exactly 11 digits.
  4. Limit a number input to 1–5.
  5. Which attribute shows a numeric keypad without changing the input type?
  6. How can one button submit the form to a different URL?

Practice Answers

  1. value is real content that gets sent; placeholder is just a hint that disappears.
  2. readonly
  3. <input name="phone" pattern="[0-9]{11}" title="11 digits">
  4. <input type="number" min="1" max="5">
  5. inputmode="numeric"
  6. Give it a formaction attribute.

Key Takeaways

  • value, placeholder, readonly, disabled, size, maxlength, autofocus shape the field.
  • required, min, max, step and pattern validate it.
  • inputmode picks the phone keyboard.
  • form* attributes let a button or input override its form.