TExcelTAD
Home › HTML › HTML Input Attributes

HTML Input Attributes

Beyond type and name, the <input> element accepts many attributes that set its starting value, limit what users can enter, give hints and change how the field behaves. Learning them saves you a lot of JavaScript.

Reference

Attribute Example Effect
value value="Hanoi" Initial value of the field
placeholder placeholder="e.g. 0901…" Grey hint shown while empty
readonly readonly Can't be edited, but is submitted
disabled disabled Can't be edited or focused and is not submitted
size size="10" Visible width in characters
maxlength / minlength maxlength="6" Limits on text length
min / max min="1" max="10" Limits for numbers and dates
step step="0.5" Allowed increments
required required Must be filled before submitting
pattern pattern="[0-9]{6}" Regular expression the value must match
multiple multiple Allow several emails or files
autofocus autofocus Focus this field when the page loads
autocomplete autocomplete="email" Hint for browser autofill
list list="cities" Connects to a <datalist>
checked checked Pre-ticks a checkbox or radio
accept accept=".pdf,image/*" File types offered in a file picker
form form="signup" Belongs to a form elsewhere on the page

value, placeholder, readonly and disabled

value is real content; placeholder is just a hint that vanishes when you type. readonly and disabled both prevent editing, but they differ in one important way: a disabled field is left out of the submitted data.

html · try it yourself
<style>
  label { display: block; margin: 6px 0; }
  pre { background: #f4f4f4; padding: 6px; }
</style>
<form id="f">
  <label>Country <input name="country" value="Vietnam" readonly></label>
  <label>Member ID <input name="memberId" value="A-1024" disabled></label>
  <label>Nickname <input name="nick" placeholder="type a nickname"></label>
  <button>Submit</button>
</form>
<pre id="out"></pre>
<script>
  document.getElementById('f').addEventListener('submit', (e) => {
    e.preventDefault();
    const pairs = [...new FormData(e.target)].map(([k, v]) => k + ' = "' + v + '"');
    document.getElementById('out').textContent = pairs.join('\n');
  });
</script>

Submit it and notice that memberId is missing and that an empty nickname is sent as an empty string — the placeholder text is never sent.

Limiting what can be typed

maxlength stops typing after a number of characters. min, max and step restrict numbers, dates and times. size only changes how wide the box looks; it does not limit input.

html · try it yourself
<form action="#">
  <p><label>PIN (max 4): <input name="pin" maxlength="4" size="4" inputmode="numeric"></label></p>
  <p><label>Rating 0–5 in halves:
    <input type="number" name="rating" min="0" max="5" step="0.5" value="3.5">
  </label></p>
  <p><label>Pick a 2026 date:
    <input type="date" name="d" min="2026-01-01" max="2026-12-31">
  </label></p>
  <button>Check</button>
</form>

Try typing 7 into the rating spinner and pressing Check: the browser reports that the value must be at most 5.

required, pattern and multiple

required blocks submission of an empty field. pattern takes a regular expression that the whole value must match; add a title to explain the rule, because the browser shows it in the error message. multiple allows comma-separated emails or several files.

html · try it yourself
<form id="g">
  <p><label>Student code (2 letters + 4 digits):
    <input name="code" required pattern="[A-Z]{2}[0-9]{4}" title="Example: IT2024" placeholder="IT2024">
  </label></p>
  <p><label>Invite emails:
    <input type="email" name="invite" multiple size="35" placeholder="a@x.com, b@y.com">
  </label></p>
  <button>Register</button>
</form>
<p id="msg"></p>
<script>
  document.getElementById('g').addEventListener('submit', (e) => {
    e.preventDefault();   // only runs if the browser's checks passed
    document.getElementById('msg').textContent = '✅ All fields are valid!';
  });
</script>

Overriding the form from a button

A submit button can override its form's settings with formaction, formmethod, formtarget, formenctype and formnovalidate. A common use is a "Save draft" button that skips validation:

html · try it yourself
<form id="post">
  <p><input name="title" required placeholder="Title (required)"></p>
  <button name="act" value="publish">Publish</button>
  <button name="act" value="draft" formnovalidate>Save draft</button>
</form>
<p id="r"></p>
<script>
  document.getElementById('post').addEventListener('submit', (e) => {
    e.preventDefault();
    document.getElementById('r').textContent = 'Clicked: ' + e.submitter.value;
  });
</script>

With an empty title, Publish is blocked but Save draft goes through. The clicked button's own name/value is sent too, so the server knows which one was pressed.

Tip: Use inputmode="numeric" for codes like PINs that are made of digits but are not really numbers. It shows a number keyboard on phones without the spinner of type="number".

Note: Every rule here runs in the browser and can be bypassed. The server must always check the data again.

Test yourself

What is the difference between readonly and disabled?

  1. There is none
  2. readonly values are submitted, disabled values are not
  3. disabled values are submitted, readonly values are not
  4. readonly hides the field

Which attribute checks the value against a regular expression?

  1. regex
  2. match
  3. pattern
  4. format

Does size="5" stop users from typing more than 5 characters?

  1. Yes
  2. No, it only sets the visible width; use maxlength to limit length
  3. Only for numbers
  4. Only in Chrome