LEARNING OBJECTIVES ⌵
- Understand how on-screen soft keyboards displace mobile viewports and introduce typing friction.
- Master the difference between semantic input types (
type="...") and virtual keyboard triggers (inputmode="..."). - Implement specialized keyboard layouts for PINs, credit cards, telephone numbers, emails, and currency fields.
- Customize the mobile keyboard action key using the WHATWG
enterkeyhintattribute. - Leverage HTML
autocompleteandautocapitalizeattributes to reduce form drop-off rates.
📖 The Mental Model & Story (Intuitive Foundation)
Imagine a physical ATM machine where, instead of a clean 10-digit numeric PIN pad, the machine presents a full 104-key QWERTY typewriter keyboard. To enter your 4-digit PIN, you must hunt through four rows of tiny letters, switch to a symbol page, and squint at microscopic digits.
This is precisely what happens on mobile devices when web forms fail to configure virtual keyboard hints. When a user taps an input field on iOS or Android, the operating system launches a software keyboard that takes over 40% to 50% of the entire display area.
+-----------------------------------+
| [Logo / Checkout] |
| Enter Card Number: [___________] |
+-----------------------------------+
| 1 2 3 4 5 6 7 8 |
| q w e r t y u i | <- Default QWERTY keyboard
| a s d f g h j | User must hunt for tiny numbers!
| z x c v b n |
| [123] [ space ] [Go] |
+-----------------------------------+
vs.
+-----------------------------------+
| [Logo / Checkout] |
| Enter Card Number: [___________] |
+-----------------------------------+
| 1 2 3 |
| 4 5 6 | <- inputmode="numeric" PIN Pad
| 7 8 9 | Gigantic, effortless thumb targets!
| 0 ⌫ |
+-----------------------------------+
By adding declarative HTML attributes (inputmode, enterkeyhint, autocomplete), you dictate the exact layout of the virtual keyboard—sparing users from tedious mode-switching and drastically boosting conversion rates.
Technical Deep Dive & Specifications
type vs. inputmode: The Crucial Distinction
Many developers mistakenly use <input type="number"> for credit cards, phone numbers, and PIN codes. This causes severe bugs:
- Leading Zeros Stripped: Entering ZIP code
02134converts to integer2134. - Scientific Notation Allowed: Typing
1e5is parsed as $100,000$. - Mouse Wheel / Arrow Keys Mutate Values: Scrolling over the field increments the number.
- Micro-Spinners Appear: Browsers inject tiny up/down stepper arrows that take up touch space.
The Golden Rule:
- Use
type="..."to define data validation semantics (e.g.,type="text",type="email",type="tel"). - Use
inputmode="..."to hint the virtual keyboard layout to the mobile operating system.
The inputmode Specification Matrix
Standardized in WHATWG HTML, inputmode accepts eight possible values:
| Value | Mobile Keyboard Displayed | Best Use Cases | Example Snippet |
|---|---|---|---|
none |
No virtual keyboard shown | Custom canvas games, in-page keypads | <input inputmode="none"> |
text |
Standard QWERTY keyboard | General sentences, user bio, street address | <input type="text" inputmode="text"> |
decimal |
Numbers + localized decimal separator (. or ,) |
Prices, currency amounts, weights, latitude/longitude | <input type="text" inputmode="decimal"> |
numeric |
Digits 0–9 PIN pad | 2FA OTP codes, credit card numbers, ZIP codes | <input type="text" inputmode="numeric" pattern="[0-9]*"> |
tel |
12-key telephone dial pad (0-9, *, #, +) |
Phone numbers, SMS contact verification | <input type="tel" inputmode="tel"> |
search |
Standard QWERTY with prominent blue "Search" 🔍 key | Site search, product lookup | <input type="search" inputmode="search"> |
email |
Standard keyboard with @ and . readily accessible |
Email address login/signup | <input type="email" inputmode="email"> |
url |
Keyboard with ., /, and .com shortcuts, space disabled |
Website URLs, domain inputs | <input type="url" inputmode="url"> |
The enterkeyhint Attribute
The enterkeyhint attribute customizes the label and visual icon on the virtual keyboard's primary action key (bottom-right):
enterkeyhint Value |
Visual Action Key (iOS / Android) | Typical User Action |
|---|---|---|
enter |
↵ Enter / Return | Inserts a newline into a <textarea>. |
done |
"Done" / Checkmark (✓) | Closes the soft keyboard upon completing input. |
go |
"Go" / Forward Arrow (➔) | Submits a form immediately or navigates to destination. |
next |
"Next" / Tab Arrow (⇥) | Advances focus directly to the next input in the form. |
previous |
"Previous" (⇤) | Returns focus to the preceding input field. |
search |
Magnifying Glass (🔍) / "Search" | Submits a search query. |
send |
Paper Airplane (✈) / "Send" | Dispatches a chat message or comment. |
The 2FA One-Time Passcode (OTP) Pattern
For seamless SMS two-factor authentication, combine inputmode="numeric" with autocomplete="one-time-code". On iOS Safari and Android Chrome, the OS automatically parses the SMS verification code received via text and displays an autofill suggestion directly above the keyboard:
<input
type="text"
name="otp"
id="otp"
inputmode="numeric"
pattern="[0-9]*"
maxlength="6"
autocomplete="one-time-code"
required
>
💻 Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Line 77 (
inputmode="decimal" enterkeyhint="next"): Opens the decimal numeric keypad (with.or,) and displays a "Next" tab arrow to smoothly focus the card number next. - Line 90–93 (
inputmode="numeric" autocomplete="cc-number"): Renders the 10-key numeric PIN pad on mobile and allows browser/OS password managers to autofill the 16-digit credit card number with one tap. - Line 105–108 (
autocomplete="one-time-code" enterkeyhint="done"): Listens for incoming SMS 2FA messages to provide seamless one-tap paste on iOS/Android, and sets the action key to "Done" to dismiss the keyboard upon completion. - Line 47 (
font-size: 1rem;/ 16px): Guarantees the input text size is at least 16px, preventing iOS Safari from triggering disruptive auto-zoom.
Expected Browser Render Output
💳 Instant Mobile Checkout
Payment Amount ($)
[ 49.99 ] <-- Pops up Decimal keypad (0-9 + .)
Card Number
[ 4532 •••• •••• 8921 ] <-- Pops up 10-key PIN pad + Card Autofill
SMS Verification Code (2FA)
[ 6-digit code ] <-- Pops up PIN pad + Auto SMS Paste
[ Complete Payment ]🏋️ Hands-On Exercise
🎯 The Challenge: Build a Frictionless Mobile Search & Contact Form
You are tasked with redesigning a mobile customer support form. The current form forces users to switch keyboards constantly between letters, numbers, and symbols, resulting in high abandonment.
Instructions:
- Create a search query input with
inputmode="search"andenterkeyhint="search". - Create a phone number input with
type="tel",inputmode="tel", andautocomplete="tel". - Create an account ID input that accepts alphanumeric characters, disables auto-capitalization (
autocapitalize="none"), disables autocorrect (autocorrect="off"), and usesenterkeyhint="go". - Ensure all inputs have labels and explicit 16px font sizes.
🏁 Starter Code Sandbox
⚠️ Common Pitfalls
- Using
<input type="number">for Credit Cards & PINs: This causes mobile browsers to strip leading zeros (e.g.,0042becomes42) and shows awkward up/down stepper arrows. Always use<input type="text" inputmode="numeric" pattern="[0-9]*">. - Forgetting
autocapitalize="none"on Email/Usernames: By default, iOS capitalizes the first character of anytype="text"field. When users type their username or custom email handle, they end up with unintended capitalization (e.g.,JohnDoe@). - Relying Solely on JavaScript Key Filtering: Intercepting
keydownevents to block non-numeric characters often breaks mobile Android IME (Input Method Editor) composition, causing characters to duplicate or get dropped.
💡 Pro Tips
- Use
autocomplete="one-time-code"for SMS OTP: This single attribute triggers native OS SMS parsing on iOS Safari and Google Chrome on Android, reducing 2FA verification drop-off by over 30%. - The W3C VirtualKeyboard API: In modern Chromium browsers, use
navigator.virtualKeyboard.overlaysContent = trueand CSSenv(keyboard-inset-height)to prevent the keyboard from resizing fixed layouts, letting you smoothly animate UI around the keyboard.
📌 Key Takeaways
- Virtual soft keyboards take up to 50% of the mobile viewport, making typing ergonomics paramount.
type="..."controls HTML semantics and validation;inputmode="..."controls the mobile keyboard layout.- Never use
type="number"for PINs or card numbers; usetype="text" inputmode="numeric" pattern="[0-9]*". - Use
enterkeyhint(search,go,send,next,done) to customize the keyboard's primary action button. - Always disable auto-capitalization and spellcheck on usernames, codes, and IDs using
autocapitalize="none"andautocorrect="off". - --