SnippetUI

OTP入力

1文字入力で自動フォーカス、バックスペースで前のボックスに戻る直感的な6桁OTP入力コンポーネントです。ペーストにも対応。

HTMLCSSJavaScript

プレビュー

6桁のOTP入力ボックス。数字を入力すると次のボックスに自動移動します。

認証コードを入力
数字を入力すると自動で次のボックスに移動します

ユースケースと特徴

  • 認証フロー: SMS・メール認証コード入力、二要素認証 (2FA) 画面に最適。

  • ペースト対応: クリップボードから数字を貼り付けると自動で各ボックスに分配される。

  • モバイル最適化: inputmode="numeric" でスマートフォンで数字キーボードを自動表示。

  • バリデーション: 数字以外の入力を replace(/[^0-9]/g, '') で除外する。

デザインとUXのポイント

  1. 52x60pxのボックスサイズ: 視認性が高くタッチ操作しやすい最適サイズ。

  2. フォーカス時の transform: scale(1.05): 現在入力中のボックスが明確に分かる。

  3. .filled クラス: 入力済みボックスのボーダー色を変えることで進捗を視覚化。

カスタマイズ方法

4桁OTPに変更


{/* input要素を4つに減らす */}

<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />

<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />

<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />

<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />

ボックスサイズの変更


.otp-box { width: 44px; height: 52px; font-size: 1.25rem; }

実装コード

<div class="otp-inputs" id="otp-inputs">

<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />

<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />

<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />

<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />

<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />

<input class="otp-box" type="text" inputmode="numeric" maxlength="1" />

</div>



<style>
.otp-inputs { display: flex; gap: 0.75rem; }

.otp-box {

width: 52px; height: 60px;

background: rgba(255,255,255,0.06);

border: 2px solid rgba(255,255,255,0.12);

border-radius: 12px;

color: white;

font-size: 1.5rem;

font-weight: 700;

text-align: center;

outline: none;

transition: border-color 0.2s, background 0.2s, transform 0.1s;

}

.otp-box:focus { border-color: #6366f1; background: rgba(99,102,241,0.1); transform: scale(1.05); }

.otp-box.filled { border-color: rgba(99,102,241,0.5); }
</style>



<script>

(function() {

var inputs = Array.from(document.querySelectorAll('#otp-inputs .otp-box'));

inputs.forEach(function(input, i) {

  input.addEventListener('input', function(e) {

    var val = e.target.value.replace(/[^0-9]/g, '');

    e.target.value = val.slice(-1);

    if (val && i < inputs.length-1) inputs[i+1].focus();

    e.target.classList.toggle('filled', !!e.target.value);

  });

  input.addEventListener('keydown', function(e) {

    if (e.key==='Backspace' && !e.target.value && i>0) {

      inputs[i-1].focus(); inputs[i-1].value=''; inputs[i-1].classList.remove('filled');

    }

  });

  input.addEventListener('paste', function(e) {

    e.preventDefault();

    var text = (e.clipboardData || window.clipboardData).getData('text').replace(/[^0-9]/g, '');

    text.split('').forEach(function(ch, j) {

      if (inputs[i+j]) { inputs[i+j].value = ch; inputs[i+j].classList.add('filled'); }

    });

    var nextEmpty = inputs.findIndex(function(inp, j) { return j >= i && !inp.value; });

    if (nextEmpty !== -1) inputs[nextEmpty].focus();

  });

});

})();

</script>

ブラウザ対応状況

| ブラウザ | 対応状況 |

|--------|--------|

| Chrome 66+ | ✅ 完全対応 |

| Firefox 63+ | ✅ 完全対応 |

| Safari 12+ | ✅ 完全対応 |

| Edge 79+ | ✅ 完全対応 |

モバイルでは inputmode="numeric" により数字キーボードが自動表示されるため、ユーザービリティが大幅に向上します。