Skip to content
Tutorials Center
My learning

Topics

Lesson 69 of 122 ยท Forms

Input Attributes

Fine-tune form inputs with value, placeholder, readonly, disabled, min, max, step, pattern, autocomplete, inputmode, enterkeyhint and other input attributes.

The type attribute decides what kind of control an <input> is. A long list of other attributes fine-tune how it behaves: what it starts with, what it accepts, how the browser can help fill it in, and which keyboard appears on a phone. Knowing them lets you build forms that are quicker to fill in and harder to get wrong, without writing JavaScript.

This lesson walks through them in groups. Some also work on <textarea> and <select>, which is noted as we go.

Starting values: value and placeholder

value sets the starting value of an input:

HTML
<label for="qty">Quantity</label>
<input type="number" id="qty" name="qty" value="1">

There is an important subtlety. The attribute is only the default. Once someone types, the live value lives in the element's value property, and the attribute stays unchanged. In JavaScript, input.value gives the current text and input.defaultValue gives the attribute. A reset button restores the default.

placeholder shows hint text while the field is empty. It is never submitted and disappears as soon as someone types:

HTML
<label for="phone">Phone</label>
<input type="tel" id="phone" name="phone" placeholder="+1 555 0100">

As you saw in Labels, Fieldsets and Legends, a placeholder is never a replacement for a label. Use it for a short example of the expected format, or not at all. placeholder also works on <textarea>.

readonly versus disabled

Both stop people from editing a field, but they are not the same:

readonly disabled
Can be focused and selected Yes No
Value can be copied Yes Usually not
Submitted with the form Yes No
Checked by validation No No
Typical look Normal, sometimes subtle Grayed out

Use readonly for a value people should see and submit but not change, such as an order number. Use disabled for a control that does not apply right now, such as a shipping address when "same as billing" is ticked.

readonly, disabled and value

Press Send: the order number is sent, the coupon is not. Click into the order number and you can select its text, but not change it. Edit the gift note, then press Reset, and the original text returns from the value attribute.

Size and length

  • size sets the visible width of a text-like input in characters (default 20). It is purely visual; CSS width is usually the better tool.
  • maxlength stops people from typing more than a number of characters.
  • minlength sets a minimum length that is checked on submit.
HTML
<label for="pin">PIN</label>
<input type="password" id="pin" name="pin" minlength="4" maxlength="8">

Both length attributes also work on <textarea>. They count UTF-16 code units, so an emoji can count as two characters.

Numbers and dates: min, max and step

For number, range and the date and time types, min and max set the allowed range, and step sets the allowed increments:

HTML
<input type="number" id="price" name="price" min="0" max="500" step="0.01">
<input type="time" id="slot" name="slot" min="09:00" max="17:00" step="1800">
<input type="date" id="trip" name="trip" min="2026-11-01" max="2026-12-20">

A few details are worth knowing:

  • The default step for number is 1, so 2.5 is rejected unless you set a step. For prices, use step="0.01".
  • step="any" allows any value.
  • For time, step is in seconds, so 1800 means half-hour slots. For date it is in days.
  • Steps count from min. With min="1" and step="2", the valid values are 1, 3, 5 and so on.

Date pickers usually gray out days outside min and max, and number spinners stop at the limits.

multiple

On type="email", multiple allows a comma-separated list of addresses. On type="file", it allows choosing several files. (On <select>, as you saw in Drop-downs and Suggestions, it allows several options.)

HTML
<label for="invite">Invite people (separate with commas)</label>
<input type="email" id="invite" name="invite" multiple>

pattern

pattern is a regular expression that the whole value must match. You do not need ^ and $: the browser always matches the entire value. Explain the rule in visible text, because the browser's error message can only say that the format does not match:

HTML
<label for="sku">Product code</label>
<input type="text" id="sku" name="sku" pattern="[A-Z]{3}-[0-9]{4}" aria-describedby="sku-hint">
<p id="sku-hint">Three capital letters, a dash and four digits, such as ABC-1234.</p>

pattern is checked only when the field has a value; combine it with required if the field must be filled in. Form Validation shows the whole validation system in action.

autocomplete

Browsers can fill in names, addresses, emails and passwords for people, which saves a lot of typing, especially on phones. The autocomplete attribute tells them exactly what each field is for, using standard tokens:

Token Field
name, given-name, family-name Full name, first name, last name
email Email address
tel Phone number
username Username or login email
current-password Password on a sign-in form
new-password Password on a sign-up or change-password form
one-time-code A code sent by text message or an authenticator app
street-address, postal-code, country-name Address parts
organization Company name
bday Date of birth

Tokens can be combined with a section prefix such as shipping or billing: autocomplete="shipping postal-code". autocomplete="off" asks the browser not to suggest previous entries, which suits fields like a one-off search code, but many browsers ignore it for login fields so that password managers keep working.

new-password versus current-password matters. On a sign-up form, new-password tells password managers to suggest a strong new password. On a sign-in form, current-password tells them to fill the saved one.

autocomplete also works on <textarea>, <select> and the <form> itself.

inputmode and enterkeyhint

On touch devices, two attributes shape the on-screen keyboard without changing what the field accepts:

inputmode picks the keyboard layout: text, numeric (digits only), decimal (digits and a decimal separator), tel, email, url, search or none. It is the right tool for digit codes that are not numbers:

HTML
<label for="code">Verification code</label>
<input type="text" id="code" name="code" inputmode="numeric" autocomplete="one-time-code" maxlength="6">

enterkeyhint changes the label of the Enter key: enter, done, go, next, previous, search or send. A chat box might use send; the last field of a checkout might use done.

Both attributes are only hints. Desktop browsers ignore them, and some phone keyboards support only a subset.

autofocus and list

autofocus focuses a control as soon as the page loads. Use it only where it is clearly what everyone wants, such as the search box on a search page. On other pages it can scroll the page unexpectedly, skip content a screen reader user would otherwise hear, and pop up the keyboard on phones. Only one element per page should have it.

list connects an input to a <datalist> of suggestions by id. It is covered in Drop-downs and Suggestions.

form: controls outside the form

Normally a control belongs to the <form> it sits inside. The form attribute connects a control elsewhere on the page to a form by its id. That helps when the layout puts a field or button far away, for example in a toolbar:

Phone-friendly attributes and the form attribute

The checkbox and the Pay now button sit outside the <form>, yet the checkbox's value is submitted and the button submits the form. On a phone, the ZIP field shows a number pad and an Enter key labeled for the last step. Try typing letters into the ZIP field and paying: pattern stops you.

Use the form attribute sparingly. Code is easier to follow when controls sit inside their form.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which field is submitted with the form?

    Read-only fields are submitted and can be focused. Disabled fields are skipped entirely.
  2. 2. A price field uses type=number with no step. Why is 4.99 rejected?

    Set step to 0.01 for prices, or to any for any value. inputmode only changes the keyboard.
  3. 3. Which autocomplete token belongs on the password field of a sign-up form?

    new-password tells password managers to suggest a fresh, strong password. current-password is for sign-in forms, and password is not a valid token.
  4. 4. What does inputmode=numeric change?

    inputmode is a keyboard hint. To actually restrict the value, use pattern (and check on the server).

Summary

  • value sets the default value; the live value is the value property. placeholder is a hint, never a label.
  • readonly fields are submitted; disabled fields are not.
  • size, minlength and maxlength handle width and length; min, max and step handle numbers, dates and times.
  • multiple allows several emails or files; pattern checks the whole value against a regular expression.
  • autocomplete tokens let browsers fill fields correctly; inputmode and enterkeyhint tune the phone keyboard.
  • Use autofocus rarely; list links a datalist; form attaches a control outside the form.

Next up: Form Attributes, the settings on the <form> element itself.