Accessible form patterns for shadcn
Rules / Forms: 15 rules and 12 guidelines
Every field in a form raises the same questions. What is it called, and does that name stay on screen? When does an error appear, where, and when does it go away? Which fields may be left empty? Can the browser fill in the address? Which keyboard does a phone show, and does Enter send the form? shadcn’s Input, Label, Checkbox and RadioGroup work with the keyboard and expose the right roles, but none of them answers those questions. The patterns on this page do.
Where forms go wrong
Each of these looks fine in a screenshot:
- A placeholder used as the label. Once the person types, the field has no visible name. FRM-001
- A label that is drawn but not connected to its control, so a screen reader announces an unnamed text box. FRM-010
- Errors collected in a banner at the top or in a toast, so nobody can tell which field is wrong. FRM-002a
- An error that shows up while the person is still typing their email address, or one that stays after it has been fixed. FRM-002b
- An asterisk that nothing explains, or required fields marked on one form and optional fields on the next. FRM-005
autocomplete="off"on a name or address field, or a password field that blocks paste. FRM-007, FRM-008- An email field typed as plain text, or a card number in
type="number", so a phone shows the wrong keyboard. FRM-014, FRM-014a, FRM-014b, guidelines (our choice) - A search field where Enter does nothing, or a checkbox whose text does nothing when clicked. FRM-015, FRM-016a, and FRM-016b, a guideline (our choice)
- A field that rejects a pasted phone number because of its spaces, or cuts a long paste at its limit without a word. FRM-017, FRM-017a, FRM-017b, FRM-017c, guidelines (our choice)
- A long page form that loses everything when the tab closes. FRM-004c, FRM-004d, guidelines (our choice)
- A checkout that asks for the delivery address twice. FRM-009
- A form sent with problems that leaves focus on the submit button, so keyboard users hunt for the problem. FRM-011, a guideline (our choice)
- A select that loads another page, or a checkbox that sends the form, as soon as its value changes. FRM-012a, FRM-012b
- A date field whose calendar opens on focus and takes focus with it, so Tab never gets past the field. FRM-013
How the rules fit together
First each field gets a name that people can see (FRM-001) and that assistive technology can read (FRM-010). Then the form shows which fields are required or optional, one way and in the label (FRM-005), and it fills in what the browser or the process already knows (FRM-007, FRM-009). Signing in has its own rule, because a password field that refuses paste can lock people out (FRM-008).
Each field also gets the keyboard its value needs: an input type for email, phone and web addresses (FRM-014), the digit keypad for digits (FRM-014a), and a text input, never a number input, for digits that are not a quantity (FRM-014b). Every field takes a paste (FRM-017) and removes the spaces and separators people write before checking (FRM-017a). A free-text field with a limit states it and shows how much is used (FRM-017b), and says in words when it is reached (FRM-017c). Enter in a single-line field sends the form (FRM-015), and a click on the text beside a checkbox or radio button changes it (FRM-016a), as on a switch (FRM-016b). All of these except FRM-015 and FRM-016a are guidelines (our choice).
When something is wrong, three texts work as one. FRM-002a says where the error goes: as text at the field. FRM-002b says when: after the person leaves a field they changed, or when they send the form, and gone as soon as the value is valid. FRM-011, a guideline (our choice), says where focus goes when a form is sent with problems: to the first problem or to a summary of them. The wording is TXT-001a.
Three texts cover what a control may set off. FRM-012a: changing a value does not send a form, open a page, window or dialog, or move focus, unless text before the control says it will. Changes in place, such as a list that filters, are allowed. FRM-012b goes further: a control that holds a choice, such as a select, radio group, checkbox or switch, only records it and may update the view in place. Going to another page or step, sending data or running a command takes a button, a link or a menu of actions, even with a notice. FRM-013: a control receiving focus never sends a form, opens a page, window or dialog, or moves focus on, and no notice changes that.
Forms in dialogs add two more questions. Each one needs a Cancel that always works and changes nothing (FRM-003). Closing one with unsaved input asks first, but only when something would be lost: FRM-004a covers full-screen dialogs, and FRM-004b, a guideline, smaller dialogs and sheets. Leaving a page raises the same question. FRM-004c asks first by every route out, and FRM-004d keeps what people entered in a longer page form and shows that it is kept; both are guidelines. The dialog patterns cover the rest of how those dialogs behave, and the button patterns cover the submit button, including when it may be unavailable.
What WCAG requires and what the judges decide
The rules on autocomplete, signing in, repeated entry, names in code and changes on input and on focus come straight from WCAG 2.2: 1.3.5 Identify Input Purpose, 3.3.8 Accessible Authentication (Minimum), 3.3.7 Redundant Entry, 4.1.2 Name, Role, Value, 3.2.2 On Input and 3.2.1 On Focus.
WCAG asks for labels (3.3.2 Labels or Instructions) but not that they stay visible at the control, and it does not say where and when errors appear, how required fields are marked, or that a dialog needs a Cancel. The judges decide those, and on most of them every judge who spoke was in support. Visible labels are a majority: two of the three judges support FRM-001. Keeping choices apart from actions even where a notice would satisfy 3.2.2 (FRM-012b) goes beyond the law, and all three judges support it. So does Enter sending the form (FRM-015), and both judges who spoke support a checkbox or radio button whose text can be clicked (FRM-016a). Asking before a small dialog or a page throws input away (FRM-004b, FRM-004c), keeping a draft (FRM-004d), moving focus to the first problem (FRM-011), and the texts on input types, switch text, paste and length limits are guidelines, our choice, because too few judges have spoken; our reasons are on each page.
Rules and guidelines
Each one has its own page with the full rule, Do and Don't cases, exceptions and the verdict. A rule is decided by WCAG 2.2 or by the judges. A guideline is our own choice. How the method works
Required by WCAG 2.2
A WCAG 2.2 Level AA criterion requires the behaviour. Where a rule adds advice of its own, it says so.
FRM-007Give fields for the person's own details the matching autocomplete valueWhen a field asks for the person's own details and WCAG 2.2 lists a matching input purpose, give the field the exact autocomplete value for that purpose and never remove it. Our own addition, which 1.3.5 does not ask for: never stop the browser or a password manager from filling the field in.FRM-008Let people sign in with paste, a password manager or a route with no testAt every step of signing in, let user name, password and code fields take a pasted value in one go and be filled by the browser or a password manager, ask for selected characters of a secret only if another route needs no such step, and give any other test, such as a CAPTCHA, a route without a test.FRM-009Fill in or offer what the person already gave earlier in the same processWithin one process, when a step needs information the person already entered or the service already showed them, either put it in the field or offer it on the same page to pick, and never make them type it again. Our own addition, which 3.3.7 does not ask for: a value put in the field stays editable.FRM-010Give every form control a name in code that contains its visible labelGive every form control a name in code that assistive technology can read. When a control has a visible text label, its name contains that text. When a group's options need the group label to make sense, that label can be read in code together with the options.FRM-012aChanging a value does not send a form, open a page or move focus unless people were told firstChanging a control's value, such as choosing an option, ticking a box or typing, does not send a form, open a page, a window or a dialog that takes focus, or move focus, unless text before the control tells people it will. Changes in place, such as a list that filters, are allowed while focus stays put.FRM-013Receiving focus never sends a form, opens a page, a window or a dialog that takes focus, or moves focus onWhen a control receives focus, by Tab, a click or a script, that alone never sends a form, opens a page, a window or a dialog that takes focus, or moves focus to another control. A notice before the control does not change this. Content that appears beside the control and leaves focus in it is allowed.
Decided by the judges
No WCAG criterion requires these. The judges who spoke support them, all or most of them.
FRM-001Give every form control a visible labelGive every form control a label that people can see at the control, beside it, above it or on its border, before, while and after they fill it in, and never let placeholder text stand in for that label.FRM-002aShow a field's error as text at the fieldWhen a field or a group of inputs has an invalid value, show the error as visible text at that field, and never only in a banner, a dialog or a tooltip somewhere else.FRM-002bShow a field's error when the person leaves the field or sends the form, and clear it as soon as the value is validShow a new error on a field only when the person leaves it after changing it or sends the form, never while they type; recheck an error that is already shown on every change and remove it as soon as the value is valid.FRM-003Give every form in a dialog a Cancel that always works and changes nothingEvery form in a dialog, sheet or other modal view has a visible Cancel button that is always enabled, is never blocked by field errors, sends nothing and leaves everything as it was before the form opened.FRM-004aAsk before a full-screen dialog throws away unsaved changes, and only when something would be lostWhen closing a full-screen dialog would throw away changes the person has not saved, ask first, whichever way they close it, with Cancel to keep everything and a button that names the discard; when nothing would be lost, close at once.FRM-005Mark required or optional fields one way, in the label, with any symbol explainedWhen a form marks required or optional fields, give every field of the same status the same marker, put it in the visible label or the group's legend as part of the accessible name, and explain a symbol in visible text no later than the first marked field.FRM-012bLet a control that holds a choice only record it, and put actions on buttons and links, even with a noticeA control that holds a choice, such as a select, radio group, checkbox, switch or filter chip, only records it and may update the view in place. Going to another page or step, sending a form or running a command takes a button, a link or a menu of actions, even with a notice.FRM-015Let Enter in a single-line field send the form, as its main button doesIn a form, pressing Enter in a single-line field, such as a text, email, number or search input, sends the form, the same as pressing its main button.FRM-016aLet a click on a checkbox's or radio button's text change it, as a click on the box doesWhen a checkbox or a radio button shows its own text beside it, a click, tap or pen press on that text checks or unchecks the checkbox, or selects the radio button, exactly as a press on the box or circle does.
Guidelines, our choice
The judges are split or too few have spoken, so we choose and give our reasons. A product that chooses otherwise is not wrong.
FRM-004bAsk before a dialog or sheet that does not fill the screen throws away unsaved changes, and only when something would be lostWhen closing a dialog or sheet that does not fill the screen would throw away changes the person has not saved, ask first, whichever way they close it. The question offers Cancel, which keeps everything, and a button that names the discard. When nothing would be lost, close at once.FRM-004cAsk before leaving a page throws away unsaved changes, and only when something would be lostWhen leaving a page would throw away unsaved form changes, ask first, by every route out: inside the product with Cancel to stay and a button naming the discard, and on closing or reloading the tab with the browser's own prompt. When nothing would be lost, leave at once.FRM-004dKeep what people enter in a longer page form when they leave, and show that it is keptOn a page form with a multi-line text field, more than one step, or five or more controls, a radio or tick-box group counting as one, keep what the person enters when they leave: save each change or keep a draft to return to. Show near the form that it is kept.FRM-011When a form is sent with problems, move focus to the first problem or to a summary of themWhen a form is sent and problems are found at its fields, in the browser or by the server, move focus to the first field with a problem, or to a summary above the form that links to each problem. This holds for every form: short or long, page or dialog, and each step of a multi-step form.FRM-014Give email, phone and web address fields the input type that matches the valueGive a field that asks only for an email address type="email", a field for a phone number type="tel" and a field for a web address type="url". A phone or tablet then shows a keyboard with the keys that value needs.FRM-014aGive a text field that takes only digits the digit keypad with inputmodeWhen a text field takes a value made of digits only, other than a phone number, give it inputMode="numeric". When it takes an amount or measurement with a decimal part, give it inputMode="decimal". A phone or tablet then shows a digit keypad.FRM-014bPut a string of digits that is not a quantity in a text input, never a number inputPut a string of digits that is not a quantity, such as a one-time code, a card number, an account number, a phone number or a postcode, in a text input: type="text", or type="tel" for a phone number. Never put it in type="number".FRM-016bLet a click on a switch's text turn it on or off, as a click on the switch doesWhen a switch shows its own text beside it, a click, tap or pen press on that text turns the switch on or off exactly as a press on the switch does.FRM-017Let every text field take a pasted value, and never cut it without saying soOutside sign-in, which FRM-008 covers, let every text field and text area take a pasted value. Never block a paste, and never cut or drop pasted characters without saying so. Removing separators from the value that is checked, as FRM-017a describes, is not such a change.FRM-017aAccept the spaces and separators people write in a number or code, and remove them before checkingWhen a field takes a number or code people write in several ways, such as a phone or card number, accept the spaces, hyphens, brackets, dots and letter case they type or paste, and remove them before checking.FRM-017bState a free-text field's length limit and show how much is usedWhen a free-text field limits how many characters or words it takes, state the limit in its label or hint and show how much is used in a count that is tied to the field in code.FRM-017cSay in words when a free-text limit is reached, and never cut text in silenceWhen a free-text field limits how many characters or words it takes, say in words when people reach or pass the limit. Never stop or cut typed or pasted text without saying so.
All rules · Is shadcn accessible? · Give the rules to an AI agent
Justified by Blackwood & Blake helps you work toward WCAG 2.2 AA. It is not a certification. Read more
Justified is an independent project. It is not affiliated with, sponsored or endorsed by the organisations whose guidance it cites. Read the full statement.
Apple is a trademark of Apple Inc., registered in the U.S. and other countries and regions.