Keyboard hints
Which input attributes to set per field so phones show the right keyboard, autofill works, and Enter does what its label says.
An <Input> from a component library is a styled <input> that passes
attributes through. Picking the attributes is left to you, and on a phone
they decide which keyboard opens, whether autofill offers the SMS code, and
whether the first letter of an email gets capitalized. Six attributes do the
work:
| Attribute | Decides | Values |
|---|---|---|
type | what the value is (validation, semantics) | email, tel, url, search, number, … |
inputmode | which virtual keyboard, nothing else | text, numeric, decimal, tel, email, url, search, none |
autocomplete | what autofill and password managers offer | email, tel, name, postal-code, one-time-code, new-password… |
autocapitalize | automatic capitals on virtual keyboards | none, sentences, words, characters |
autocorrect | automatic word correction | on, off |
spellcheck | red underlines | true, false |
enterkeyhint | the label on the Enter key | enter, done, go, next, previous, search, send |
Per field
Open this page on a phone to see each keyboard. The attributes are listed under every field.
| Field | type | inputmode | autocomplete | autocapitalize | autocorrect / spellcheck | enterkeyhint |
|---|---|---|---|---|---|---|
| Full name | name | words | off | next | ||
email | email | (always off) | off | next | ||
| Phone | tel | tel | next | |||
| Postcode | postal-code | characters | off | next | ||
| Code from SMS | numeric | one-time-code | done | |||
| Amount | decimal | off | done | |||
| Search | search | search | ||||
| Chat message | sentences | send |
Notes on the less obvious cells:
- Names get "corrected" into dictionary words, so autocorrect and spellcheck go off.
- Postcodes stay
text: UK and Canadian ones contain letters, and a numeric keyboard would lock those users out.characterssaves a shift press per letter. one-time-codemakes Safari offer the code from an incoming SMS above the keyboard. Chrome on Android has a separate API for the same job, WebOTP.- Passwords:
current-passwordon sign-in,new-passwordon sign-up and change-password forms. The second one is what makes the password manager suggest a strong password instead of filling the old one. See Password. - Two addresses on one page get prefixes:
shipping postal-code,billing postal-code. email,urlandpasswordinputs never autocapitalize or autocorrect, whatever the attributes say.autocapitalizeonly affects virtual keyboards and dictation, never a physical keyboard. Chrome and Safari default tosentences, Firefox tonone.
Enter only changes its label
enterkeyhint="next" puts "Next" on the key, but pressing it still does what
Enter always does in a form: submit it. Untick the checkbox above and press
Enter in the first field — the form submits with one field filled. Moving
focus is your job:
const onKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
// Enter also confirms an IME composition (Chinese, Japanese input).
if (event.key !== "Enter" || event.nativeEvent.isComposing) return;
if (event.currentTarget.enterKeyHint !== "next") return;
event.preventDefault();
nextInput?.focus();
};The isComposing check matters: someone typing Chinese or Japanese presses
Enter to pick a character, and without the check every pick jumps to the next
field.
Numbers that aren't quantities
type="number" is for amounts you'd increment — quantity, age, guests. Card
numbers, codes, account numbers and postcodes are strings of digits, and
type="number" hurts them:
4111-1111stays on screen, butvaluebecomes an empty string, so JavaScript that reads the field sees nothing (Chrome also drops typed spaces without a word);eis accepted, because1e5is a number;- the mouse wheel over a focused field changes the value;
maxlengthis ignored;- some screen readers and voice-control tools announce it as a spin button or can't fill it.
Type 4111-1111 into both fields:
GOV.UK switched its design system to type="text" inputmode="numeric" for
this reason. Their recommendation also adds pattern="[0-9]*", which older
iOS versions needed to show the digit keypad before they supported
inputmode; it's harmless to keep, but it also turns on validation, so make
sure the pattern matches what you accept (spaces in a card number won't).
Decimal input and the comma
inputmode="decimal" shows the decimal separator of the device's region,
not of your page. A German phone shows , on an English site; an English
phone shows . on a German one. So the parser has to accept both, and the
page locale should only break real ties — a single separator followed by
exactly three digits, like 1.234, is a thousand in German and one point
two-three-four in English.
3.5 → $3.50
Install with the shadcn CLI, or copy the source below.
npx shadcn@latest add https://ui-patterns.stereobooster.com/r/parse-decimal.jsonfunction decimalSeparator(locale?: string) {
return (
new Intl.NumberFormat(locale)
.formatToParts(1.1)
.find((part) => part.type === "decimal")?.value ?? "."
);
}
/**
* Parses an amount typed with either "." or "," as the decimal separator.
* The virtual keyboard shows the separator of the device region, which can
* differ from the page locale, so both must be accepted. The page locale
* only breaks the tie for a single separator followed by exactly three
* digits ("1.234"). Returns `null` for anything that isn't a number.
*/
export function parseDecimal(input: string, locale?: string): number | null {
const s = input.replace(/[\s' ]/g, "").replace(/^−/, "-");
if (!/^-?[\d.,]*\d[\d.,]*$/.test(s)) return null;
const last = Math.max(s.lastIndexOf("."), s.lastIndexOf(","));
if (last === -1) return Number(s);
const sep = s[last];
const other = sep === "." ? "," : ".";
const head = s.slice(0, last);
const digitsAfter = s.length - last - 1;
let decimal: string | null;
if (head.includes(other)) {
if (head.includes(sep)) return null;
decimal = sep;
} else if (head.includes(sep)) {
decimal = null;
} else if (digitsAfter === 3) {
decimal = decimalSeparator(locale) === sep ? sep : null;
} else {
decimal = sep;
}
if (decimal === null && digitsAfter !== 3) return null;
const group = decimal === null ? sep : other;
const ungrouped = s.split(group).join("");
const value = Number(decimal ? ungrouped.replace(decimal, ".") : ungrouped);
return Number.isFinite(value) ? value : null;
}When the tie is broken by locale, show the parsed value back ("€1,234.00") so a wrong guess is visible before submit.
When the keyboard covers the page
By default the keyboard shrinks only the visual viewport. The layout stays the same size, so a button fixed to the bottom of the screen ends up behind the keyboard. The viewport meta tag can change that:
<meta
name="viewport"
content="width=device-width, interactive-widget=resizes-content"
/>| Value | Effect |
|---|---|
resizes-visual | default; only the visual viewport shrinks |
resizes-content | the layout viewport shrinks, fixed elements move up |
overlays-content | nothing resizes; the keyboard draws over the page |
Not every browser supports interactive-widget; those that don't keep the
default, so test fixed footers on real devices.