JavaScript Notes
Learn to build an accessible OTP/PIN input field in JavaScript with auto-focus, backspace navigation, paste support, and timer-based resend functionality.
What You'll Learn
- How to create multiple single-digit input boxes that feel like one input
- How to auto-advance focus when a digit is entered
- How to move focus back with Backspace
- How to support paste of a 6-digit code in one action
- How to implement a countdown timer for resend functionality
Project Overview
OTP (One-Time Password) fields are the 4-6 digit code boxes you see on login and verification screens. Each box accepts exactly one digit, and the cursor jumps to the next box automatically, making the experience feel polished and native.
Component Structure
How It Works — Flow Diagram
Step-by-Step Build
Step 1 — HTML Structure
Step 2 — Generate Input Boxes
Step 3 — Auto-Advance on Input
Step 4 — Backspace Navigation
Step 5 — Paste Handler
Step 6 — Verify Button & Get OTP Value
Step 7 — Countdown Timer for Resend
Console Output
// User types "1" input event on box[0] → value = "1" → focus box[1] // User types "2" input event on box[1] → value = "2" → focus box[2] // User presses Backspace on empty box[2] keydown → box[2] is empty → clear box[1], focus box[1] // User pastes "123456" paste event → distributed: boxes = ["1","2","3","4","5","6"] All boxes filled → verifyBtn.disabled = false // User clicks Verify with "123456" getOTP() → "123456" Correct! → resultMsg: "✅ Code verified successfully!" // User clicks Verify with "999999" Incorrect → shake animation → boxes cleared → focus box[0]
Edge Cases to Handle
| Situation | Expected Behavior |
|---|---|
| Type non-digit character | Blocked by e.preventDefault() |
| Paste partial code (3 digits) | Fill first 3 boxes, focus 4th |
| Paste letters | Strip non-digits with regex |
| Backspace on first box | Clear value, stay focused |
| Verify with incomplete code | Button stays disabled |
Challenge Yourself
- SMS auto-fill —
autocomplete="one-time-code"already helps on mobile — test it - Error animation — animate boxes shaking left/right on wrong code
- Masked input — hide entered digits as
●like a PIN pad - Custom length — make
OTP_LENGTHa configuration option (4, 6, or 8 digits) - API verification — replace the hardcoded check with a real
fetchPOST request
Best Practices
- Set
inputMode="numeric"andpattern="[0-9]*"for proper mobile keyboard - Handle both
inputandkeydown— they serve different purposes - Paste handling must call
e.preventDefault()to take control of the clipboard data - The
autocomplete="one-time-code"attribute enables browser/OS auto-fill - Always call
focus()programmatically to guide the user's cursor seamlessly
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Build a JavaScript OTP Input Field - Step by Step Project Tutorial.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this JavaScript Master Course topic.
Search Terms
javascript-complete-guide, javascript master course, javascript, complete, guide, mini, projects, otp
Related JavaScript Master Course Topics