Robust Multi-Digit Verification Input
In many secure applications, multi-factor authentication (MFA) relies on One-Time Passcodes (OTPs) sent to a user's device. A smooth and intuitive OTP input experience is critical for user satisfaction and security. This problem challenges you to build a highly usable and accessible component for entering multi-digit verification codes, mirroring the quality expected in high-stakes user flows.
Your goal is to implement a reusable React component, OTPInput, that facilitates the entry of a multi-digit verification code.
OTPInput that accepts a length prop (number, e.g., 6) and an onChange prop (function).length individual input fields, each designed to accept a single digit.onChange Callback: The onChange prop should be called with the complete OTP string (e.g., "123456") whenever the full code is entered. If the code is incomplete, onChange should be called with an empty string ""._ or empty).OTPInput component renders N individual input elements based on the length prop.i automatically moves focus to field i+1.i moves focus to field i-1 and clears its content.i clears its content without moving focus.N digits into any field correctly populates all subsequent fields.onChange callback is invoked with the complete N-digit string when all fields are filled.onChange callback is invoked with "" when the OTP is incomplete.aria-label or aria-describedby attributes.Constraints
length prop will always be a positive integer between 4 and 8.OTPInput component is structured, its API, and its ability to be reused.Loading editor...