行内校验反馈
Inline Validation
输入框附近的提示随校验结果更新,帮助修正或确认当前值。
适用场景
就地帮助用户纠正邮箱、密码等表单字段。 把提示贴在对应字段旁边,在用户离开输入框或暂停输入时给出反馈。
应用示例
填写美国收货地址时,用户只输入四位邮编就离开输入框,字段下方提示「请输入五位邮编」。补齐最后一位后,提示消失。
相关设计技巧
输入以后,字段名还在吗?AI 提示词
给【字段】添加行内校验。首次移开焦点时检查,在输入框旁给出纠正建议,提示出现后随编辑更新,并用 aria-describedby 关联。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>行内校验反馈</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.example { width: min(100%, 228px); position: relative; display: grid; gap: 10px; }
.panel { background: #292240; border: 1px solid #675785; border-radius: 11px; padding: 10px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.label { margin: 0; font-size: 13px; font-weight: 650; }
.detail, .caption { margin: 0; color: #d7cbea; font-size: 11px; line-height: 1.35; }
.caption { text-align: center; }
.control { border: 1px solid #8874ae; background: #392e57; border-radius: 7px; padding: 5px 9px; min-height: 32px; color: #f6efff; cursor: pointer; font-size: 11px; font-weight: 650; }
.control:hover { background: #514174; }
.control.primary { color: #261b3f; background: #d1bfff; border-color: #e3d8ff; }
.control.primary:hover { background: #e3d8ff; }
.control:disabled { opacity: .55; cursor: default; }
.field { width: 100%; min-width: 0; padding: 6px 8px; border: 1px solid #875fa8; border-radius: 6px; color: #f6efff; background: #1e1732; font-size: 12px; }
.field::placeholder { color: #c2abd8; }
[hidden] { display: none !important; }
.input-sample { display: grid; gap: 7px; }
.input-sample[data-state="invalid"] .field { border-color: #ff9cbd; }
.input-sample[data-state="valid"] .field { border-color: #b2f0d0; }
.input-sample[data-state="valid"] .detail { color: #b2f0d0; }
@media (prefers-reduced-motion: reduce) {
.example *, .example *::before, .example *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
</style>
</head>
<body>
<main class="demo">
<div class="example"><div class="panel input-sample"><label class="label" for="demo-email">工作邮箱</label><input class="field" id="demo-email" type="email" autocomplete="off" placeholder="name@example.com" aria-describedby="email-result"><p class="detail" id="email-result" aria-live="polite">输入邮箱地址。</p></div></div>
</main>
<script>
const panel = document.querySelector('.input-sample');
const field = panel.querySelector('input');
const hint = panel.querySelector('.detail');
function validate() {
const empty = !field.value.trim();
const valid = field.validity.valid;
panel.dataset.state = empty ? 'empty' : valid ? 'valid' : 'invalid';
field.setAttribute('aria-invalid', String(!empty && !valid));
hint.textContent = empty ? "输入邮箱地址。" : valid ? "邮箱格式正确。" : "请使用 name@example.com 这样的格式。";
}
field.addEventListener('blur', validate);
field.addEventListener('input', () => {
if (panel.dataset.state) validate();
});
</script>
</body>
</html>UI Skills 原创示例