OTP Input Component
React.js
hard
30 mins
Description
Create a 4-digit OTP (One-Time Password) input component using React. The OTP should be entered one digit per input box. The focus should auto-move as the user types or deletes, and the component should support pasting a full OTP. It should also reject any non-numeric characters.
Constraints & Edge Cases
- Only numeric input is allowed.
- If a box is empty and backspace is pressed, move focus to the previous box.
- Paste (e.g.
"1234") should fill all 4 boxes correctly.
Requirements
- Create a component that renders 4 input boxes, each accepting only one numeric digit.
- Automatically move focus to the next input when a digit is entered.
- Automatically move focus to the previous input when backspace is pressed on an empty field.
- If the user pastes the OTP (e.g.
"1234"), each box should fill with a digit. - Once all 4 digits are entered, trigger a callback
onChangeOTPand send the full OTP string. - Disallow any non-numeric input.
Reference UI

Preview what you need to build
Companies:
paytm
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
