Shake Error Feedback
A form field that shakes briefly and shows a clear message when the input is invalid, with a no-motion fallback.
AI prompt
Clean UI mockup of a sign-in form with an email field marked invalid: red border, a small red warning icon inside the field, a red message below reading Enter a valid email like [email protected], subtle motion lines at both sides of the field suggesting a short horizontal shake, a primary Continue button under it, light theme, flat modern style. Negative: heavy shadows, clutter, text errors, device frames. Reduced motion: static design.About this pattern
Definition
A quick horizontal shake says no, like shaking your head. It draws the eye to the field that needs attention. It must never be the only signal: the field also needs a red border, an icon or text, and a message that explains how to fix the problem.
Purpose & UX principles
- Shake along the x axis for about 300ms with three to four small steps of 4 to 8 pixels.
- Add aria-invalid and connect the message with aria-describedby.
- Move focus to the field so keyboard users land on the problem.
- Re-trigger the animation by removing and re-adding the class.
Interaction states
- Enter an email and press Submit; try empty, invalid and valid values.
Motion specification
- The shake lasts 300ms.
- Reduced motion: no shake; the border flashes once instead.
Responsive behaviour
- Under reduced motion, skip the shake and rely on the border and text.
- Do not shake on every keystroke; validate on submit or on blur.
Code
<div class="bx"><form id="fm" novalidate><label class="fl" for="em">Email</label><input id="em" type="email" autocomplete="off" placeholder="[email protected]" aria-describedby="er"><p class="er" id="er" role="alert"></p><button class="mini" type="submit">Continue</button></form><label class="sw"><input id="sk" type="checkbox" checked> Shake animation</label></div>: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}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.fl{font-size:12px;font-weight:600}#em{display:block;width:100%;box-sizing:border-box;height:38px;margin:4px 0;border:1.5px solid var(--line);border-radius:10px;padding:0 12px;font:inherit;font-size:14px;background:var(--surface);color:var(--text)}#em[aria-invalid=true]{border-color:#d92d20;background:#fff6f5}.er{color:#c0261a;font-size:12px;min-height:18px;margin:0 0 8px}.shake{animation:sh .3s}@keyframes sh{20%{transform:translateX(-7px)}40%{transform:translateX(6px)}60%{transform:translateX(-4px)}80%{transform:translateX(3px)}}.flash{animation:fl .4s}@keyframes fl{50%{box-shadow:0 0 0 3px rgba(217,45,32,.35)}}.mini{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12px;font-weight:600;cursor:pointer}.mini:disabled{opacity:.4;cursor:default}.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}@media (prefers-reduced-motion:reduce){.shake{animation:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const fm=document.getElementById('fm'),em=document.getElementById('em'),er=document.getElementById('er'),sk=document.getElementById('sk'),calm=matchMedia('(prefers-reduced-motion:reduce)').matches;if(calm){sk.checked=false;sk.disabled=true;}
fm.addEventListener('submit',e=>{e.preventDefault();const v=em.value.trim();let m='';if(!v)m='Enter your email address.';else if(!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(v))m='Enter a valid email like [email protected].';
if(m){em.setAttribute('aria-invalid','true');er.textContent=m;em.classList.remove('shake','flash');void em.offsetWidth;em.classList.add(sk.checked&&!calm?'shake':'flash');em.focus();}else{em.removeAttribute('aria-invalid');er.textContent='';er.style.color='#1f7a45';er.textContent='\u2713 Looks good.';setTimeout(()=>{er.textContent='';er.style.color='';},1800);}});
em.addEventListener('input',()=>{if(em.getAttribute('aria-invalid')){em.removeAttribute('aria-invalid');er.textContent='';}});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>Shake Error Feedback — 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}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.fl{font-size:12px;font-weight:600}#em{display:block;width:100%;box-sizing:border-box;height:38px;margin:4px 0;border:1.5px solid var(--line);border-radius:10px;padding:0 12px;font:inherit;font-size:14px;background:var(--surface);color:var(--text)}#em[aria-invalid=true]{border-color:#d92d20;background:#fff6f5}.er{color:#c0261a;font-size:12px;min-height:18px;margin:0 0 8px}.shake{animation:sh .3s}@keyframes sh{20%{transform:translateX(-7px)}40%{transform:translateX(6px)}60%{transform:translateX(-4px)}80%{transform:translateX(3px)}}.flash{animation:fl .4s}@keyframes fl{50%{box-shadow:0 0 0 3px rgba(217,45,32,.35)}}.mini{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12px;font-weight:600;cursor:pointer}.mini:disabled{opacity:.4;cursor:default}.sw{display:flex;gap:6px;align-items:center;font-size:12px;color:var(--muted)}@media (prefers-reduced-motion:reduce){.shake{animation:none}}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><form id="fm" novalidate><label class="fl" for="em">Email</label><input id="em" type="email" autocomplete="off" placeholder="[email protected]" aria-describedby="er"><p class="er" id="er" role="alert"></p><button class="mini" type="submit">Continue</button></form><label class="sw"><input id="sk" type="checkbox" checked> Shake animation</label></div>
</div>
<script>
const fm=document.getElementById('fm'),em=document.getElementById('em'),er=document.getElementById('er'),sk=document.getElementById('sk'),calm=matchMedia('(prefers-reduced-motion:reduce)').matches;if(calm){sk.checked=false;sk.disabled=true;}
fm.addEventListener('submit',e=>{e.preventDefault();const v=em.value.trim();let m='';if(!v)m='Enter your email address.';else if(!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(v))m='Enter a valid email like [email protected].';
if(m){em.setAttribute('aria-invalid','true');er.textContent=m;em.classList.remove('shake','flash');void em.offsetWidth;em.classList.add(sk.checked&&!calm?'shake':'flash');em.focus();}else{em.removeAttribute('aria-invalid');er.textContent='';er.style.color='#1f7a45';er.textContent='\u2713 Looks good.';setTimeout(()=>{er.textContent='';er.style.color='';},1800);}});
em.addEventListener('input',()=>{if(em.getAttribute('aria-invalid')){em.removeAttribute('aria-invalid');er.textContent='';}});
</script>
</body></html>