On this page11 sections
Lecture 037: Input Attributes
| Category | 6: Forms |
| Lecture | 037 of 048 |
| Estimated video length | 14 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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,maxlengthandautofocus - Validate with
required,min,max,stepandpattern - Use
multiple,listandinputmode - Override form settings with
form,formaction,formmethodandformnovalidate
1. Starting Values and Hints
Example
<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 boxplaceholder: 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
<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? | |
|---|---|---|---|
readonly | No | Yes | Yes |
disabled | No | No | No |
Submit the example to see that coupon isn't sent.
3. Size and Length
Example
<p><label>PIN <input name="pin" size="4" maxlength="4" minlength="4"></label></p>size: visible width, in charactersmaxlength/minlength: allowed number of characters
4. Validation: required, min, max, step, pattern
Example: try submitting with wrong values:
<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
<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:
<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>| Attribute | Overrides the form's… |
|---|---|
formaction | action |
formmethod | method |
formtarget | target |
formenctype | enctype |
formnovalidate | Validation (skip it for this button) |
An input can even live outside its form with the form attribute:
<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
- What's the difference between
valueandplaceholder? - A field must be sent but not edited.
readonlyordisabled? - Write an input that accepts exactly 11 digits.
- Limit a number input to 1–5.
- Which attribute shows a numeric keypad without changing the input type?
- How can one button submit the form to a different URL?
Practice Answers
valueis real content that gets sent;placeholderis just a hint that disappears.readonly<input name="phone" pattern="[0-9]{11}" title="11 digits"><input type="number" min="1" max="5">inputmode="numeric"- Give it a
formactionattribute.
Key Takeaways
value,placeholder,readonly,disabled,size,maxlength,autofocusshape the field.required,min,max,stepandpatternvalidate it.inputmodepicks the phone keyboard.form*attributes let a button or input override its form.