Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk5 min

React Challenge #65: Build an OTP Input

A practical walkthrough of React challenge #65, covering six controlled OTP fields, keyboard focus transitions, paste behavior, demo-code status, and reset.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Build the OTP control as six controlled, single-digit inputs backed by one six-character value array, plus references to the inputs for focus changes. The key is to treat typing, backspace, arrow keys, paste, and reset as explicit state-and-focus transitions. Derive the status from the values instead of maintaining a second status state.

ReactChallenges lists “OTP Input” as challenge #65, rated intermediate with a 60-minute estimate. Its goal is a six-field interface that accepts digits, moves focus as the user enters them, supports pasting a complete code, and checks the result against a demo code. This is a UI exercise, not a complete authentication system.

As an Amazon Associate I earn from qualifying purchases.

What should the React OTP input do?

Render six controlled inputs, each holding at most one digit. On mount, focus the first field. A valid digit fills the active field and advances focus when another field is available; non-numeric characters should not become part of the value. The challenge also specifies both arrow-key navigation and two distinct backspace behaviors.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Interaction Value change Focus change
Type a digit Put the digit in the active position Advance to the next field, if one exists
Type a non-numeric character Leave the value unchanged Do not advance
Backspace in a filled field Clear that field Stay in the current field
Backspace in an empty field Clear the preceding field Move to the preceding field, if one exists
ArrowLeft or ArrowRight Leave values unchanged Move one field in the indicated direction, within the six-field range
Paste a code Distribute digits across available positions; ignore digits after position six Update focus consistently with the pasted positions
Reset Clear all six positions Return to the first field

How should state and focus be represented?

Controlled values

Use an array with six entries, such as six empty strings initially. Each input displays its corresponding entry, and every accepted change updates that entry. This makes it straightforward to filter input before it is stored and to determine whether all positions are filled.

Input references

Keep a reference for each input, typically in an array of refs. A small focus helper can focus a requested index after checking that it is between the first and sixth positions. The values and the focused element are separate pieces of the interaction: updating state alone does not move keyboard focus.

For this challenge, six separate controlled inputs are required. A different design sometimes stores one code string and renders segmented-looking cells, which can simplify value handling but changes the input model. It does not meet the stated requirement to render six inputs.

How do I move focus between OTP fields?

Handle keyboard events at the input level and explicitly choose whether each event changes a value, moves focus, or both. When the component takes responsibility for a key action, prevent the browser’s default behavior where necessary so that native editing does not conflict with the component’s transition.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Typing a digit

Validate the incoming character, update only the active position, then focus the next input if one exists. Do not advance beyond the last field. A controlled change handler should ensure rejected characters never persist in the rendered value.

Backspace and arrows

Backspace depends on the current field’s value. If it contains a digit, clear that position and leave focus there. If it is empty and there is a previous field, clear the preceding position and focus it. At the first field, there is no preceding position to clear.

ArrowLeft and ArrowRight should move focus by one position without changing any values. Keep movement inside the valid index range; pressing an arrow at either end should not attempt to focus a nonexistent input.

How do I handle pasting a one-time code?

Handle the paste event as a multi-value operation rather than assuming the browser will generate one ordinary key event per pasted digit. Read the clipboard text, retain its digits, and place them into the six positions in order. Ignore any digits beyond the sixth position, as the challenge specifies. Prevent the default paste when the component is applying its own distribution so browser insertion does not compete with the controlled update.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Decide deliberately where focus should land after a paste. A practical behavior is to focus the next empty position, or remain on the final field when all six positions have been filled. The documented requirements call for distribution and truncation; they do not prescribe a particular post-paste focus position.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How should verification status be calculated?

Derive the status from the current values and the challenge’s demo code rather than storing status separately. This prevents status from drifting out of sync after typing, paste, or reset. The comparison is meaningful only once all six fields contain a digit.

  • Fewer than six filled fields: “Waiting for code…”
  • All six filled and matching the demo code: “Code verified”
  • All six filled and not matching: “Invalid code. Try again.”

The comparison validates only against the exercise’s demo value. It does not issue or verify a real one-time password with a server, and it does not establish properties such as code expiry, rate limiting, or identity verification.

What improves mobile OTP entry?

The challenge recommends inputMode="numeric" and autoComplete="one-time-code" on the first field. These attributes can help browsers offer an appropriate keyboard or suggest a received code. MDN describes autocomplete as a hint about the expected field information and identifies one-time-code with OTPs used as an additional sign-in factor. Autofill behavior depends on the browser and device; these attributes are not a guarantee that a code will appear or fill every field automatically.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What should you check before calling the challenge complete?

The official challenge test list covers these behaviors. It is a checklist of documented cases, not a claim that this article’s implementation has been independently run against them.

  • Exactly six fields render, and the first receives focus on mount.
  • The demo code is rendered as expected by the exercise.
  • Typing a digit updates the active field and advances focus.
  • Non-numeric input does not persist.
  • Backspace clears the current filled field, while backspace in an empty field clears and focuses the preceding field.
  • Arrow keys navigate between fields without altering values.
  • Pasting distributes digits and a matching complete code receives the verified status.
  • A complete incorrect code receives the invalid status.
  • Extra pasted digits do not exceed the six available positions.
  • Reset clears values, focuses the first field, and restores “Waiting for code…” status.

The source for challenge requirements, guidance, and documented tests is ReactChallenges challenge #65. The meaning of the autocomplete token is described in MDN’s autocomplete reference.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.