Validation States
Inline field validation with clear error, success and helper text states.
AI prompt
Build a password field with inline validation. Rules: at least 8 characters, contains a number. Validate on blur; once an error has been shown, re-validate on every input. States: neutral, valid (accent border and a check icon on the right), invalid (red border #ff6b6b, alert icon on the right, message below). The message is an element with id referenced by aria-describedby, role="alert" when shown, and explains the first failing rule ("Use at least 8 characters" or "Add at least one number"). Set aria-invalid appropriately. When the field becomes invalid play a 300ms three-cycle shake of 4px; the message fades and slides in over 180ms. Include a helper line when neutral ("8+ characters and a number"). A submit button is disabled until the field is valid. Skip shake and slide under prefers-reduced-motion.About this pattern
Definition
The visual and textual feedback a form field gives about its value: neutral, valid, or invalid with a specific message. Errors are shown next to the field and announced to assistive technology.
Purpose & UX principles
- Validate after the user leaves the field (on blur), not while they are still typing, then re-check live once an error is shown.
- Say what is wrong and how to fix it ("Use at least 8 characters"), not just "Invalid".
- Do not use colour alone: pair it with an icon and text.
Interaction states
- Neutral, focused, valid (green border, check icon), invalid (red border, alert icon, message), disabled.
- The input has
aria-invalid="true"andaria-describedbypointing at the message; the message isrole="alert"when it first appears.
Motion specification
- Message: height and opacity, 180ms ease-out, with a 4px upward slide.
- Invalid field: a brief 3-cycle horizontal shake of 4px over 300ms.
- Reduced motion: no shake, no slide.
Responsive behaviour
- Messages sit directly below the field and wrap onto multiple lines instead of truncating.
- Reserve space for the message or animate its height to avoid pushing the layout abruptly.
Code
<form class="f" onsubmit="return false" novalidate>
<label for="pw">Password</label>
<div class="box" id="box"><input id="pw" type="password" autocomplete="new-password" aria-describedby="msg"><span class="ic" id="ic" aria-hidden="true"></span></div>
<p id="msg" class="msg help">8+ characters and a number.</p>
<button class="go" id="go" disabled>Create account</button>
</form>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
.f{width:min(340px,100%);padding:22px;border:1px solid var(--line);border-radius:18px;background:var(--surface)}
label{display:block;margin-bottom:8px;font-size:14px;font-weight:600}
.box{position:relative}
.box input{width:100%;height:48px;padding:0 44px 0 14px;border-radius:12px;border:1px solid var(--line);background:var(--bg);color:var(--text);font:inherit;font-size:16px;outline:none;transition:border-color .16s}
.box input:focus{border-color:var(--muted)}
.box.valid input{border-color:#1a7f4b}
.box.invalid input{border-color:var(--danger)}
.box.shake{animation:sh .3s}
.ic{position:absolute;right:14px;top:14px;width:20px;height:20px;font-weight:800;text-align:center;line-height:20px}
.valid .ic::before{content:"✓";color:#1a7f4b}
.invalid .ic::before{content:"!";color:var(--danger);border:2px solid var(--danger);border-radius:50%;display:block;line-height:16px;font-size:12px}
.msg{margin:8px 2px 0;font-size:13px;min-height:20px}
.help{color:var(--muted)}.err{color:var(--danger);animation:in .18s ease-out}
.go{margin-top:16px;width:100%;height:46px;border-radius:12px;border:0;background:var(--accent);color:var(--accent-ink);font:inherit;font-weight:700;cursor:pointer}
.go:disabled{opacity:.4;cursor:not-allowed}
@keyframes sh{25%{transform:translateX(-4px)}50%{transform:translateX(4px)}75%{transform:translateX(-4px)}}
@keyframes in{from{opacity:0;transform:translateY(-4px)}}
@media (prefers-reduced-motion:reduce){.box.shake,.err{animation:none}}const pw=document.getElementById('pw'),box=document.getElementById('box'),msg=document.getElementById('msg'),go=document.getElementById('go');
let touched=false;
function check(){return pw.value.length<8?'Use at least 8 characters.':!/\d/.test(pw.value)?'Add at least one number.':'';}
function show(){
const e=check(),was=box.classList.contains('invalid');
box.classList.toggle('valid',!e&&pw.value!=='');box.classList.toggle('invalid',!!e);
pw.setAttribute('aria-invalid',!!e);
if(e){msg.textContent=e;msg.className='msg err';msg.setAttribute('role','alert');if(!was){box.classList.remove('shake');void box.offsetWidth;box.classList.add('shake');}}
else{msg.className='msg help';msg.removeAttribute('role');msg.textContent=pw.value?'Looks good.':'8+ characters and a number.';}
go.disabled=!!e||!pw.value;
}
pw.addEventListener('blur',()=>{touched=true;if(pw.value||touched)show();});
pw.addEventListener('input',()=>{if(touched)show();else go.disabled=!!check();});One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Validation States — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
.f{width:min(340px,100%);padding:22px;border:1px solid var(--line);border-radius:18px;background:var(--surface)}
label{display:block;margin-bottom:8px;font-size:14px;font-weight:600}
.box{position:relative}
.box input{width:100%;height:48px;padding:0 44px 0 14px;border-radius:12px;border:1px solid var(--line);background:var(--bg);color:var(--text);font:inherit;font-size:16px;outline:none;transition:border-color .16s}
.box input:focus{border-color:var(--muted)}
.box.valid input{border-color:#1a7f4b}
.box.invalid input{border-color:var(--danger)}
.box.shake{animation:sh .3s}
.ic{position:absolute;right:14px;top:14px;width:20px;height:20px;font-weight:800;text-align:center;line-height:20px}
.valid .ic::before{content:"✓";color:#1a7f4b}
.invalid .ic::before{content:"!";color:var(--danger);border:2px solid var(--danger);border-radius:50%;display:block;line-height:16px;font-size:12px}
.msg{margin:8px 2px 0;font-size:13px;min-height:20px}
.help{color:var(--muted)}.err{color:var(--danger);animation:in .18s ease-out}
.go{margin-top:16px;width:100%;height:46px;border-radius:12px;border:0;background:var(--accent);color:var(--accent-ink);font:inherit;font-weight:700;cursor:pointer}
.go:disabled{opacity:.4;cursor:not-allowed}
@keyframes sh{25%{transform:translateX(-4px)}50%{transform:translateX(4px)}75%{transform:translateX(-4px)}}
@keyframes in{from{opacity:0;transform:translateY(-4px)}}
@media (prefers-reduced-motion:reduce){.box.shake,.err{animation:none}}
</style></head>
<body><div class="stage">
<form class="f" onsubmit="return false" novalidate>
<label for="pw">Password</label>
<div class="box" id="box"><input id="pw" type="password" autocomplete="new-password" aria-describedby="msg"><span class="ic" id="ic" aria-hidden="true"></span></div>
<p id="msg" class="msg help">8+ characters and a number.</p>
<button class="go" id="go" disabled>Create account</button>
</form>
</div>
<script>
const pw=document.getElementById('pw'),box=document.getElementById('box'),msg=document.getElementById('msg'),go=document.getElementById('go');
let touched=false;
function check(){return pw.value.length<8?'Use at least 8 characters.':!/\d/.test(pw.value)?'Add at least one number.':'';}
function show(){
const e=check(),was=box.classList.contains('invalid');
box.classList.toggle('valid',!e&&pw.value!=='');box.classList.toggle('invalid',!!e);
pw.setAttribute('aria-invalid',!!e);
if(e){msg.textContent=e;msg.className='msg err';msg.setAttribute('role','alert');if(!was){box.classList.remove('shake');void box.offsetWidth;box.classList.add('shake');}}
else{msg.className='msg help';msg.removeAttribute('role');msg.textContent=pw.value?'Looks good.':'8+ characters and a number.';}
go.disabled=!!e||!pw.value;
}
pw.addEventListener('blur',()=>{touched=true;if(pw.value||touched)show();});
pw.addEventListener('input',()=>{if(touched)show();else go.disabled=!!check();});
</script>
</body></html>