05유효성 값 실시간 전환 — 자릿수 값이 채워질 때마다 번호 전체의 유효성 값이 계산되어 표시 값이 바뀌는 방식
숫자 값이 한 자리씩 채워질 때마다 전체 번호의 유효성 값이 다시 계산되어, 통과하면 표시 값이 확인 모양으로 바뀝니다.
쓰임결제 폼의 카드 번호 실시간 검증, 회원가입의 카드 등록 확인
luhnSumcl__badge--onstroke-dasharray

<main class="stage">
<div class="cl is-demo" id="cl">
<label class="cl__field">
<input class="cl__input" id="clInput" type="text" inputmode="numeric" autocomplete="off" spellcheck="false" maxlength="16" value="">
<span class="cl__badge" id="clBadge">
<svg viewBox="0 0 20 20" width="14" height="14" aria-hidden="true">
<path id="clCheck" d="M4 10.5l3.5 3.5L16 5.5" fill="none" stroke="currentColor" stroke-width="2.6" stroke-linecap="round" stroke-linejoin="round" pathLength="1" stroke-dasharray="1" stroke-dashoffset="1"/>
</svg>
</span>
</label>
<div class="cl__sum" id="clSum">가중 합 값 0</div>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
// 계산 겹: 자리별 가중 합(룬 체크섬)을 매 입력마다 다시 계산해 유효성 값을 판정한다(index.html).
@include stage($stage-ink);
.cl {
width: 100%;
max-width: 300px;
display: grid;
gap: $sp-3;
}
.cl__field {
position: relative;
display: block;
}
.cl__input {
width: 100%;
box-sizing: border-box;
padding: $sp-3 $sp-10 $sp-3 $sp-4;
border-radius: $r-control;
border: 0;
background: $subject-cream;
color: $subject-ink;
font-family: "SFMono-Regular", Consolas, monospace;
font-weight: 600;
font-size: 16px;
letter-spacing: .05em;
&:focus { outline: 2px solid $subject-blue-light; outline-offset: 2px; }
}
.cl__badge {
position: absolute;
right: $sp-3;
top: 50%;
transform: translateY(-50%) scale(.6);
width: 26px;
height: 26px;
border-radius: 50%;
background: rgba($subject-ink, .14);
color: transparent;
display: grid;
place-items: center;
opacity: .5;
transition: transform $dur-quick $ease-spring, background-color $dur-quick $ease-out, color $dur-quick $ease-out, opacity $dur-quick $ease-out;
#clCheck { transition: stroke-dashoffset $dur-enter $ease-out; }
}
.cl__badge--on {
background: $tick-up;
color: $subject-ink;
opacity: 1;
transform: translateY(-50%) scale(1);
#clCheck { stroke-dashoffset: 0; }
}
.cl__sum {
color: rgba($subject-cream, .72);
font-weight: 600;
font-size: 13px;
font-family: "SFMono-Regular", Consolas, monospace;
}
@media (max-width: 340px) {
.cl__input { padding: $sp-2 $sp-8 $sp-2 $sp-3; font-size: 14px; }
}
@media (prefers-reduced-motion: reduce) {
.cl__badge, .cl__badge #clCheck { transition: none; }
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #17141a;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.cl {
width: 100%;
max-width: 300px;
display: grid;
gap: 12px;
}
.cl__field {
position: relative;
display: block;
}
.cl__input {
width: 100%;
box-sizing: border-box;
padding: 12px 40px 12px 16px;
border-radius: 12px;
border: 0;
background: #fff7e6;
color: #17141a;
font-family: "SFMono-Regular", Consolas, monospace;
font-weight: 600;
font-size: 16px;
letter-spacing: 0.05em;
}
.cl__input:focus {
outline: 2px solid #5b8cf8;
outline-offset: 2px;
}
.cl__badge {
position: absolute;
right: 12px;
top: 50%;
transform: translateY(-50%) scale(0.6);
width: 26px;
height: 26px;
border-radius: 50%;
background: rgba(23, 20, 26, 0.14);
color: transparent;
display: grid;
place-items: center;
opacity: 0.5;
transition: transform 220ms cubic-bezier(0.2, 0.8, 0.2, 1), background-color 220ms ease-out, color 220ms ease-out, opacity 220ms ease-out;
}
.cl__badge #clCheck {
transition: stroke-dashoffset 600ms ease-out;
}
.cl__badge--on {
background: #3ddc97;
color: #17141a;
opacity: 1;
transform: translateY(-50%) scale(1);
}
.cl__badge--on #clCheck {
stroke-dashoffset: 0;
}
.cl__sum {
color: rgba(255, 247, 230, 0.72);
font-weight: 600;
font-size: 13px;
font-family: "SFMono-Regular", Consolas, monospace;
}
@media (max-width: 340px) {
.cl__input {
padding: 8px 32px 8px 12px;
font-size: 14px;
}
}
@media (prefers-reduced-motion: reduce) {
.cl__badge, .cl__badge #clCheck {
transition: none;
}
}(function () {
var root = document.getElementById('cl');
var input = document.getElementById('clInput');
var badge = document.getElementById('clBadge');
var sumLabel = document.getElementById('clSum');
// 계산 겹: 자리별 가중 합(오른쪽부터 한 자리씩 건너 2배) — 룬 체크섬을 매 입력마다 다시 계산한다.
function luhnSum(digits) {
var sum = 0, dbl = false;
for (var i = digits.length - 1; i >= 0; i--) {
var d = digits.charCodeAt(i) - 48;
if (dbl) { d *= 2; if (d > 9) d -= 9; }
sum += d;
dbl = !dbl;
}
return sum;
}
function paint(digits) {
var sum = digits.length ? luhnSum(digits) : 0;
var valid = digits.length >= 13 && sum % 10 === 0;
sumLabel.textContent = '가중 합 값 ' + sum;
badge.classList.toggle('cl__badge--on', valid);
}
input.addEventListener('input', function () {
root.classList.remove('is-demo');
stopAutoplay();
var digits = input.value.replace(/\D/g, '').slice(0, 16);
input.value = digits;
paint(digits);
});
// 자동 루프: 유효 번호를 한 자리씩 채웠다가, 마지막 자리를 잘못된 값으로 바꿔 무효 상태도 보여준다.
var VALID = '9999999999999995';
var INVALID = '9999999999999990';
var SEQ = [];
for (var i = 0; i <= VALID.length; i++) SEQ.push(VALID.slice(0, i));
SEQ.push(INVALID);
SEQ.push(VALID);
for (var k = VALID.length - 1; k >= 0; k--) SEQ.push(VALID.slice(0, k));
var si = 0;
var autoTimer = null;
function autoTick() {
var digits = SEQ[si];
input.value = digits;
paint(digits);
si = (si + 1) % SEQ.length;
}
function stopAutoplay() { if (autoTimer) { clearInterval(autoTimer); autoTimer = null; } }
var reduce = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!reduce) { autoTimer = setInterval(autoTick, 180); autoTick(); }
else { autoTick(); }
})();import { useEffect, useRef } from "react";
import styles from "./LuhnLiveCheckmark.module.scss";
type Box<T> = { current: T };
const CHECK_PATH = "M4 10.5l3.5 3.5L16 5.5";
export default function LuhnLiveCheckmark() {
const rootRef = useRef(null) as unknown as Box<HTMLDivElement | null>;
useEffect(() => {
const root = rootRef.current;
if (!root) return;
const input = root.querySelector(`.${styles.input}`) as HTMLInputElement | null;
const badge = root.querySelector(`.${styles.badge}`) as HTMLElement | null;
const sumLabel = root.querySelector(`.${styles.sum}`) as HTMLElement | null;
if (!input || !badge || !sumLabel) return;
// 계산 겹: 자리별 가중 합(오른쪽부터 한 자리씩 건너 2배) — 룬 체크섬을 매 입력마다 다시 계산한다.
function luhnSum(digits: string) {
let sum = 0, dbl = false;
for (let i = digits.length - 1; i >= 0; i--) {
let d = digits.charCodeAt(i) - 48;
if (dbl) { d *= 2; if (d > 9) d -= 9; }
sum += d;
dbl = !dbl;
}
return sum;
}
function paint(digits: string) {
const sum = digits.length ? luhnSum(digits) : 0;
const valid = digits.length >= 13 && sum % 10 === 0;
sumLabel!.textContent = "가중 합 값 " + sum;
badge!.classList.toggle(styles["badge--on"], valid);
}
function onInput() {
root!.classList.remove(styles["is-demo"]);
stopAutoplay();
const digits = input!.value.replace(/\D/g, "").slice(0, 16);
input!.value = digits;
paint(digits);
}
input.addEventListener("input", onInput);
// 자동 루프: 유효 번호를 한 자리씩 채웠다가, 마지막 자리를 잘못된 값으로 바꿔 무효 상태도 보여준다.
const VALID = "9999999999999995";
const INVALID = "9999999999999990";
const SEQ: string[] = [];
for (let i = 0; i <= VALID.length; i++) SEQ.push(VALID.slice(0, i));
SEQ.push(INVALID);
SEQ.push(VALID);
for (let k = VALID.length - 1; k >= 0; k--) SEQ.push(VALID.slice(0, k));
let si = 0;
let autoTimer: number | null = null;
function autoTick() {
const digits = SEQ[si];
input!.value = digits;
paint(digits);
si = (si + 1) % SEQ.length;
}
function stopAutoplay() {
if (autoTimer !== null) { window.clearInterval(autoTimer); autoTimer = null; }
}
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
if (!reduce) { autoTimer = window.setInterval(autoTick, 180); autoTick(); }
else { autoTick(); }
return () => {
stopAutoplay();
input.removeEventListener("input", onInput);
};
}, []);
return (
<div ref={rootRef} className={`${styles.stage} ${styles.cl} ${styles["is-demo"]}`}>
<label className={styles.field}>
<input className={styles.input} type="text" inputMode="numeric" autoComplete="off" spellCheck={false} maxLength={16} defaultValue="" />
<span className={styles.badge}>
<svg viewBox="0 0 20 20" width="14" height="14" aria-hidden="true">
<path className={styles.check} d={CHECK_PATH} fill="none" stroke="currentColor" strokeWidth={2.6} strokeLinecap="round" strokeLinejoin="round" pathLength={1} strokeDasharray={1} strokeDashoffset={1} />
</svg>
</span>
</label>
<div className={styles.sum}>가중 합 값 0</div>
</div>
);
}05 유효성 값 실시간 전환 — 자릿수 값이 채워질 때마다 번호 전체의 유효성 값이 계산되어 표시 값이 바뀌는 방식 — 측정 누적면적 0.766% · 움직인 프레임 14/23GODRICH 원작 · zip 에 SCSS·React 포함 · 카드 번호 입력 UI 9종 — 자릿수 값마다 반응이 달라지는 아홉 가지 방식
이 데모가 속한 글
카드 번호 입력 UI 9종 — 자릿수 값마다 반응이 달라지는 아홉 가지 방식카드 번호 입력 ui는 자릿수 값이 채워지는 상태에 따라 화면 여러 곳의 값이 함께 바뀌는 인터랙션인데, 아홉 개 중 하나는 입력된 숫자 값이 네 자리에 이를 때마다 그 사이 간격 값이 자동으로 생기고, 하나는 자릿 이 글의 데모 전부 보기
이 효과가 마음에 들면 — 비슷한 데모
입력 UI08 단위 플립 전환 — 단위가 바뀐 상태가 되면 카드가 뒤집히며 90도 지점에서 표시 값이 바뀌는 스텝퍼숫자 스텝퍼 UI 패턴 9종 — 길게 누를수록 빨라지는 것부터 눈금에 붙는 것까지
입력 UI04 선택 개수 롤 전환 — 선택된 개수 값이 오르내릴 때마다 배지 숫자의 자리 값이 위아래로 굴러가듯 바뀌는 방식커스텀 체크박스 9종 — 체크 값으로 바뀌는 아홉 가지 방식
입력 UI01 글자 수 경계 채움 — 입력 값의 글자 수가 정해진 여러 경계 값을 넘을 때마다 강도 값이 다음 단계로 오르는 방식비밀번호 강도 UI 9종 — 강도 값이 계산되는 아홉 가지 방식