Form Error Shake
An input with a red border and a shake animation for invalid states.
Please enter a valid email address.
<style>
@keyframes input-shake {
0%, 100% { transform: translateX(0); }
20%, 60% { transform: translateX(-6px); }
40%, 80% { transform: translateX(6px); }
}
.error-shake { animation: input-shake 0.45s ease both; }
</style>
<div class="w-full max-w-sm">
<label for="err-email" class="mb-1.5 block text-sm font-medium text-slate-700 dark:text-slate-200">Email address</label>
<input id="err-email" type="email" value="not-an-email" class="error-shake w-full rounded-lg border-2 border-red-500 bg-red-50 px-4 py-2.5 text-sm text-slate-900 outline-none placeholder:text-slate-400 focus:border-red-600" />
<p class="mt-1.5 flex items-center gap-1.5 text-xs font-medium text-red-600">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" class="h-3.5 w-3.5"><circle cx="12" cy="12" r="9"/><path d="M12 7v6"/><circle cx="12" cy="16.5" r="0.5" fill="currentColor"/></svg>
Please enter a valid email address.
</p>
</div>Free to use in personal and commercial projects. No attribution required.
Variants
Same component, different looks. Preview each one live and copy the code you like.
Password error
Use the password variant on signup forms where weak passwords are the most common error.
Password must be at least 8 characters.
Success state
Pair the error with this success state so valid fields get positive feedback too.
Email looks good.
Compact inline
The compact labelless version fits inline editing and table cell inputs.
That username is already taken.
About this form error shake
A shake is the universal body language for no. When a form field fails validation, this pattern combines a red border, a light red fill, a plain-language error message, and a short horizontal shake that draws the eye exactly where the problem is.
The input-shake keyframes alternate translateX between -6px and 6px twice over 0.45 seconds, which reads as a firm wobble rather than a vibration. The error-shake class plays it once on render. In a real form you would add the class when validation fails and remove it on the next keystroke so it can fire again.
Use it on login, signup, and checkout fields the moment validation fails. Never shake without the message: motion alone does not tell a screen reader user or a distracted user what went wrong.
A shake is body language for no, understood across every culture without a word of copy. When a field fails validation, the combination of a red border, a plain-language message, and a short horizontal shake drags the eye exactly to the problem. It turns an abstract error summary into a physical location on the screen.
The wobble is a keyframe sequence nudging the input a few pixels side to side twice in under half a second, fast enough to read as a shake rather than a slide. The error-shake class plays it once on render. In a real form you add the class when validation fails and remove it on the next keystroke, so the animation can replay on the next failed submit.
The mistake is shaking on every keystroke during live validation, punishing the user mid-thought for a half-typed email. Each shake yanks attention to a problem the user was already fixing, and after three shakes the animation becomes wallpaper. Validate on blur or submit, and reserve the shake for the moment the user commits. Motion should mark the verdict, not narrate the typing.
Cranking the shake distance is the amateur move: at 20 or 30 pixels the field looks like it is glitching rather than objecting, and the whole form feels unstable. The 6px travel in this snippet is deliberate, firm enough to catch the eye, small enough to read as intentional. If the shake needs to be bigger to be noticed, the real problem is that nothing else marks the error; fix the message and the border first.
Where to use it
- ✓ Login form validation
- ✓ Signup field errors
- ✓ Checkout address forms
- ✓ Password strength failures
Customization tips
Reduce the 6px travel to 4px for a subtler shake. Swap red-500 for amber-500 for warnings instead of hard errors. Pair with aria-invalid and aria-describedby pointing at the message for screen readers.
Design notes
When to use
The shake belongs on form validation failures where the user needs an unmissable signal that something is wrong, like an invalid email on submit. The red border pinpoints the field while the shake grabs attention, and the message below explains exactly how to fix it. It works because it combines location, attention, and instruction in one place.
What to avoid
Do not shake on every keystroke, validating while typing and shaking each time feels like being scolded. Avoid shaking without a clear error message, motion without explanation just alarms people. And do not replay the shake on focus alone, it should fire on failed validation, not on arrival.
Accessibility
Connect the error message to the input with aria-describedby and set aria-invalid on the field so screen readers announce the problem. Respect prefers-reduced-motion by showing the red border and message without the shake.
Frequently asked questions
How do I shake an input on invalid submit with Tailwind?
Add a keyframe class like error-shake when validation fails, then remove it after the animation ends so it can replay. Toggle it with JavaScript on submit; CSS alone cannot detect validity timing.
Should the shake loop or play once?
Play it once. A looping shake feels like an alarm and annoys users. One firm shake on submit, then a steady red border until the field is fixed.
Is a red border enough for accessibility?
No. Color alone is not enough. Include a text message describing the error and link it with aria-describedby so assistive technology announces it.
How do I replay the shake animation on every invalid submit?
Remove the shake class, force a reflow by reading offsetWidth, then re-add the class in your submit handler. CSS animations do not restart if the class never leaves. Alternatively listen for animationend and strip the class there, so the element is always ready for the next invalid submit.
Should I turn off the shake for users who prefer reduced motion?
Yes. Inside a prefers-reduced-motion reduce block, set animation to none and rely on the static error cues instead: red border, error icon, and message text. Rapid shaking can genuinely bother users with vestibular sensitivities, and the static cues carry the same meaning without the motion.
Should the whole form shake or just the invalid field?
Shake only the invalid input. Shaking the whole form punishes the fields the user already filled correctly and makes it harder to spot which one failed. Pair the shake with an inline error message directly under the field so the fix is obvious.