Form Control Attributes
Beyond type and name, form controls accept attributes that set default values, give hints, help browsers autofill, choose the right on-screen keyboard and control editing. Used well, they make forms much faster and less frustrating to fill in.
value and placeholder
value is a real pre-filled value that will be submitted. placeholder is a hint shown only while the field is empty. Placeholders aren't labels: they vanish as soon as someone types, often have low contrast, and are skipped by some assistive technologies. Use them for examples of format, alongside a proper label.
autocomplete
The autocomplete attribute tells the browser exactly what a field is for, so it can offer saved names, addresses, emails and passwords — saving users a lot of typing:
| Value | Field |
|---|---|
name, given-name, family-name | Names |
email, tel, username | Contact and account |
address-line1, address-line2, address-level2 (city), postal-code, country — or street-address on a multi-line <textarea> | Address |
current-password, new-password, one-time-code | Login and security |
cc-name, cc-number, cc-exp, cc-csc | Payment cards |
bday, organization | Other personal details |
off | Don't suggest anything (for unique values like a search for a new product code) |
new-password lets password managers suggest a strong password; current-password lets them fill a saved one.
inputmode and enterkeyhint
On phones, inputmode chooses the on-screen keyboard without changing validation, and enterkeyhint changes the label on the Enter key:
Values include numeric, decimal, tel, email, url and search.
readonly and disabled
readonly | disabled | |
|---|---|---|
| User can edit | No | No |
| Can be focused and copied | Yes | No |
| Submitted with the form | Yes | No |
| Looks | Normal | Greyed out |
Size and length
size and cols set the visible width in characters (CSS width is usually better). maxlength stops typing at a limit; minlength is checked on submit.
Focus and spelling
autofocusfocuses a field when the page loads. Use it only when the form is clearly the page's sole purpose (like a search page) — otherwise it disorients screen-reader users.spellcheck="false"turns off spell-checking for codes, usernames and email addresses.autocapitalize="off"stops phones capitalising the first letter of usernames.
Controls outside the form
The form attribute associates a control with a form by its id, even if the control sits elsewhere in the page:
Buttons can also override the form's settings with formaction, formmethod and formnovalidate — handy for a "Save draft" button next to "Publish".