<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script>
<script type="text/babel">
const { useState, useRef } = React;
const otpLength = 6; // You can change this to your desired OTP length
const otpInputs = Array(otpLength).fill(null);
const inputRefs = otpInputs.map(() => useRef(null));
const [otp, setOtp] = useState(new Array(otpLength).fill(''));
const handleInputChange = (e, index) => {
const value = e.target.value;
if (!/^\d*$/.test(value)) return; // Only allow numeric input
if (index < otpLength - 1 && value !== '') {
inputRefs[index + 1].current.focus();
const handleBackspace = (e, index) => {
if (e.key === 'Backspace' && index > 0 && otp[index] === '') {
inputRefs[index - 1].current.focus();
<div className='container'>
<h3>React Input Mask Google-style OTP</h3>
<div className="otp-container">
{otpInputs.map((_, index) => (
onChange={(e) => handleInputChange(e, index)}
onKeyDown={(e) => handleBackspace(e, index)}
ReactDOM.render(<App />, document.getElementById("app"));
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
/* Style individual input fields */
transition: border-color 0.2s;
/* Style focused input field */
.otp-container input:focus {
/* Change the border color when the input is focused */
/* Style filled input field */
.otp-container input:valid {
/* Change the border color when the input is filled */