UI Patterns
Input

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:

AttributeDecidesValues
typewhat the value is (validation, semantics)email, tel, url, search, number, …
inputmodewhich virtual keyboard, nothing elsetext, numeric, decimal, tel, email, url, search, none
autocompletewhat autofill and password managers offeremail, tel, name, postal-code, one-time-code, new-password…
autocapitalizeautomatic capitals on virtual keyboardsnone, sentences, words, characters
autocorrectautomatic word correctionon, off
spellcheckred underlinestrue, false
enterkeyhintthe label on the Enter keyenter, 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.

autocomplete="name"autocapitalize="words"autocorrect="off"spellcheck="false"enterkeyhint="next"
type="email"autocomplete="email"spellcheck="false"enterkeyhint="next"
type="tel"autocomplete="tel"enterkeyhint="next"
autocomplete="postal-code"autocapitalize="characters"autocorrect="off"spellcheck="false"enterkeyhint="next"
inputmode="numeric"autocomplete="one-time-code"enterkeyhint="done"

Fieldtypeinputmodeautocompleteautocapitalizeautocorrect / spellcheckenterkeyhint
Full namenamewordsoffnext
Emailemailemail(always off)offnext
Phoneteltelnext
Postcodepostal-codecharactersoffnext
Code from SMSnumericone-time-codedone
Amountdecimaloffdone
Searchsearchsearch
Chat messagesentencessend

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. characters saves a shift press per letter.
  • one-time-code makes 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-password on sign-in, new-password on 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, url and password inputs never autocapitalize or autocorrect, whatever the attributes say.
  • autocapitalize only affects virtual keyboards and dictation, never a physical keyboard. Chrome and Safari default to sentences, Firefox to none.

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-1111 stays on screen, but value becomes an empty string, so JavaScript that reads the field sees nothing (Chrome also drops typed spaces without a word);
  • e is accepted, because 1e5 is a number;
  • the mouse wheel over a focused field changes the value;
  • maxlength is 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.json
lib/parse-decimal.ts
function 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"
/>
ValueEffect
resizes-visualdefault; only the visual viewport shrinks
resizes-contentthe layout viewport shrinks, fixed elements move up
overlays-contentnothing 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.

Sources

On this page