03日付の自動スラッシュ入力欄
数字だけ入力しても、2桁ごとに自動でスラッシュが入り2026/09/11のようになります。
使いどころ生年月日の入力・予約日の手入力
YYYY/MM/DD숫자만 허용자동 슬래시

<main class="stage">
<div class="dt is-demo" id="wrap">
<label class="dt__label" for="dt-input">生年月日</label>
<input class="dt__input" id="dt-input" type="text" inputmode="numeric" placeholder="YYYY/MM/DD" aria-describedby="dt-hint">
<p class="dt__hint" id="dt-hint">数字だけ入力すればスラッシュが自動的に入ります</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// ── 변수 3개 ──────────────────────────────────────────
$duration: $dur-quick; // 테두리 색 전환 시간
$easing: $ease-out; // 포커스 이징
$color: $stage-orange; // 포커스 강조 색
// ── 데모 무대 ──────────────────────────────────────────
@include stage($stage-orange);
.dt {
box-sizing: border-box;
width: min(272px, 84vw);
background: #fff;
border-radius: $r-card;
padding: $sp-4;
box-shadow: $shadow-raised;
&__label {
display: block;
margin: 0 0 $sp-1;
font-weight: 700;
font-size: 12px;
line-height: 1;
font-family: inherit;
color: $stage-ink;
}
&__input {
box-sizing: border-box;
width: 100%;
border: 2px solid transparent;
border-radius: $r-control;
padding: $sp-3 $sp-4;
font-weight: 600;
font-size: clamp(14px, 4.2vw, 18px);
font-family: inherit;
letter-spacing: .04em;
color: $stage-ink;
background: rgba(23, 20, 26, .06);
transition: border-color $duration $easing, background $duration $easing;
&:focus {
outline: none;
border-color: $color;
background: #fff;
}
}
&__hint {
margin: $sp-1 0 0;
font-size: 11px;
line-height: 1.4;
font-family: inherit;
color: rgba($stage-ink, .72);
}
}
@media (prefers-reduced-motion: reduce) {
.dt__input {
transition: none;
}
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #ff4d1f;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.dt {
box-sizing: border-box;
width: min(272px, 84vw);
background: #fff;
border-radius: 16px;
padding: 16px;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
}
.dt__label {
display: block;
margin: 0 0 4px;
font-weight: 700;
font-size: 12px;
line-height: 1;
font-family: inherit;
color: #17141a;
}
.dt__input {
box-sizing: border-box;
width: 100%;
border: 2px solid transparent;
border-radius: 12px;
padding: 12px 16px;
font-weight: 600;
font-size: clamp(14px, 4.2vw, 18px);
font-family: inherit;
letter-spacing: 0.04em;
color: #17141a;
background: rgba(23, 20, 26, 0.06);
transition: border-color 220ms ease-out, background 220ms ease-out;
}
.dt__input:focus {
outline: none;
border-color: #ff4d1f;
background: #fff;
}
.dt__hint {
margin: 4px 0 0;
font-size: 11px;
line-height: 1.4;
font-family: inherit;
color: rgba(23, 20, 26, 0.72);
}
@media (prefers-reduced-motion: reduce) {
.dt__input {
transition: none;
}
}(function () {
var wrap = document.getElementById('wrap');
var input = document.getElementById('dt-input');
function format(raw) {
var digits = raw.replace(/\D/g, '').slice(0, 8);
var out = digits.slice(0, 4);
if (digits.length > 4) out += '/' + digits.slice(4, 6);
if (digits.length > 6) out += '/' + digits.slice(6, 8);
return out;
}
input.addEventListener('input', function () {
input.value = format(input.value);
});
function stopDemo() {
if (!wrap.classList.contains('is-demo')) return;
wrap.classList.remove('is-demo');
clearInterval(timer);
}
input.addEventListener('pointerdown', stopDemo);
input.addEventListener('keydown', stopDemo);
var digits = '20260911';
var raw = '';
var phase = 'focus';
var pauseUntil = 0;
var timer = setInterval(function () {
var now = Date.now();
if (now < pauseUntil) return;
if (phase === 'focus') {
input.focus({ preventScroll: true });
phase = 'type';
return;
}
if (phase === 'type') {
if (raw.length < digits.length) {
raw += digits[raw.length];
input.value = raw;
input.dispatchEvent(new Event('input', { bubbles: true }));
} else {
phase = 'hold';
pauseUntil = now + 900;
}
return;
}
if (phase === 'hold') {
raw = '';
input.value = '';
input.dispatchEvent(new Event('input', { bubbles: true }));
input.blur();
phase = 'blurhold';
pauseUntil = now + 500;
return;
}
if (phase === 'blurhold') {
phase = 'focus';
}
}, 140);
})();import { useState } from "react";
import styles from "./DateAutoSlashInput.module.scss";
export type DateAutoSlashInputProps = {
label?: string;
hint?: string;
};
function formatDate(raw: string): string {
const digits = raw.replace(/\D/g, "").slice(0, 8);
let out = digits.slice(0, 4);
if (digits.length > 4) out += "/" + digits.slice(4, 6);
if (digits.length > 6) out += "/" + digits.slice(6, 8);
return out;
}
export default function DateAutoSlashInput({
label = "생년월일",
hint = "숫자만 입력하면 슬래시가 자동으로 붙어요",
}: DateAutoSlashInputProps) {
const [value, setValue] = useState("");
const onChange = (e: any) => {
setValue(formatDate(e.target.value));
};
return (
<div className={styles.dt}>
<label className={styles.label} htmlFor="dt-input">
{label}
</label>
<input
className={styles.input}
id="dt-input"
type="text"
inputMode="numeric"
placeholder="YYYY/MM/DD"
aria-describedby="dt-hint"
value={value}
onChange={onChange}
/>
<p className={styles.hint} id="dt-hint">
{hint}
</p>
</div>
);
}03 日付の自動スラッシュ入力欄 — 計測 累積面積 8.603% · 動いたフレーム 10/23GODRICH オリジナル · SCSS と React は zip に同梱 · 会員登録 フォーム デザイン 入力欄9選 — キーボード操作対応
このデモが属する記事
会員登録 フォーム デザイン 入力欄9選 — キーボード操作対応会員登録 フォーム デザインの良し悪しは送信ボタンではなく入力欄で決まります。ここに並べた9つは、登録画面が実際に求める入力欄で、メールアドレス欄からクリアボタンまで、コードを貼ればそのまま動きます。 この記事のデモをすべて見る


